Is there someway to respawn an NPC in roblox studio? - lua

So, I'm coding this fighting game in roblox studio. Now I have made an NPC, but I want to script it to be able to respawn a few seconds after dead, but I don't know how to check if an NPC is dead or how to respawn one and make it join its body parts again.
Thank you, and I hope you can help.

You can check the health of the NPC from the Health property of its Humanoid in order to see if it's still alive, or if it's dead.
game.workspace.<NPC>.Humanoid.Health
Now in order to check if it's dead, you can do:
if game.workspace.<NPC>.Humanoid.Health <= 0 then
--NPC is dead; respawn
end
In order to 'respawn' the NPC, duplicate the NPC (in workspace) and place the copy in ReplicatedStorage. Now whenever the NPC died, you can replace it with a new one by cloning the one in ReplicatedStorage to workspace:
if game.workspace.<NPC>.Humanoid.Health <= 0 then
wait(2) -- If you want to wait before the NPC respwns, change '2' for the amount of seconds you want to wait
game.ReplicatedStorage.<NPC>:Clone().Parent = game.workspace
end
Finally, wrap this all in a while loop so it can continuously check the health of the NPCs.
while wait() do
if game.workspace.<NPC>.Humanoid.Health <= 0 then
wait(2) -- If you want to wait before the NPC respwns, change '2' for the amount of seconds you want to wait
game.ReplicatedStorage.<NPC>:Clone().Parent = game.workspace
end
end

Related

In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000 cash

In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000
cash,and tool dispears.I already have made a leaderboard for the cash,and already have my tool in server storoge.
First off, you can start by using a .Touched event which fires every time an object is touched. A .Touched event can carry one parameter; the object that touched it. For example, if a player (R15) stepped on a block on the floor, the object that hit the part might be Left Foot. Here is what the skeleton of a .Touched function looks like:
<Part>.Touched:Connect(function(hit)
print(hit.Name) --As per the example above, this would print "Left Foot"
end)
Since any object can touch the part, but not every one has a Backpack, you'll want to first check if the object that hit your part is from an actual player. You can do this by looking for a Humanoid, which would be a sibling of the part that touched. You can do this by adding:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print(hit.Name) --As per the example above, this would print "Left Foot"
end
end)
Now that we have verified that a player was what touched our part, we can use the function of game.Players called GetPlayerFromCharacter(). A Character is just your player in game.workspace, so by calling this function, it gets your player in game.Players from your character. This is similar to game.Players:FindFirstChild(<Player>.Name). Either one work, but I will be using the first one for this example. Added to your code, here is what it should now look like:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
end
end)
As you may know, Backpack is a direct child of the player in game.Players, so we can use player.Backpack to find our Backpack. Now we can do the same thing that we did to check for a humanoid, to check for our tool:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Backpack = player.Backpack
if Backpack:FindFirstChild("<Tool Name>") then
player.leaderstats.<Money>.Value += 2000 --Gives them 2000 cash
end
end
end)
If you cloned the tool into the player's Backpack previously, you can do Backpack.<Tool Name>:Destroy() to get rid of it/make it dissapear.

A script that will only run when a player joins the game

game.Players.PlayerAdded:Connect(function(player)
game.StarterPlayer.CameraMaxZoomDistance = 0
end)
I want the player to be in first perspective only when they first join the game.
After they die they should be in third person. How do I go about this?
game.Players.PlayerAdded:Connect(function(player)
game.StarterPlayer.CameraMaxZoomDistance = 0
end)
after line 1 and before game.StarterPlayer add a characteraddedfunction like so
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
game.StarterPlayer.CameraMaxZoomDistance = (number you want for default)
end)
end)
this waits for character to respawn and gets that character and puts it in the character variable, then the game waits for the Humanoid object to appear inside the character, then it waits for the Died event to take place which is the event that watches for a respawn or "death" then it sets the cameradistance back to normal based on the number you put.

Roblox Studio Lua: Cash For Kill Script Issue

So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. I've already got a script, see below. I tried everything on the Internet, but NOTHING works. Even the ones from the toolbox. But instead of giving the killer the cash, it gives the killed person cash! I don't want it to be a death for kill script! Here is the code I have another question like this, it will not help. It wasn't useful(It has the leaderboard, and the killed person gets +10 cash.):
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the
player died.
end
end
end)
end)
end)
Thanks in advance and I hope you guys can help!
There's no way to determine that player A killed player B without keeping track of that information manually.
For example, if player A shoots a gun, the bullet that it spawns could have an added ObjectValue with a Value of player A's humanoid.
When the bullet collides with player B, you would have an easy reference back to player A and can credit them for the KO.

Roblox Studio Lua: Making A Cash For Kill Script

So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. The name on the leaderboard should be Cash. Can someone please make a script for this? I've tried everything on the web, so I hope you guys can help. Please include the leaderboard Cash in the script. Thanks in advance!
Update
I've included the code for the script, but instead of giving me the cash, it gives the killed person cash. How can I fix this?
Here is the code:
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the player died.
end
end
end)
end)
end)
You increase the amount of money in currency1. But currency1 belongs to player, who is the who has .Died!
Assuming creator is an ObjectValue whose Value is the Player instance of the killer, we can get that player's "Cash" to increase:
....
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + 10
end
end
end
......
This is a bit of a complex task if you have no experience with scripting. This requires your weapon system to keep track of who kills, and feed that into the leaderboard.
If you use default Roblox weapons, this functionality is likely already in your weapons. If you are making custom weapons, you need to implement this yourself.
I suggest you check out the Roblox Wiki for an example of Leaderboards. This is a relevant article about Leaderboards.
There are also going to be plenty of leaderboards in the Toolbox that you can edit to your needs. The default Leaderboard found in the "Roblox Sets" section of the Leaderboard increases a counter named "Kills" upon kills (with default Roblox weapons), and you can edit this to increase Cash instead. However - again - depends on how you keep track of your kills.
Next time please provide code and/or more detailed description of what you have already tried, so it is easier for us to help you understand or give you pointers. Right now it looks like you haven't really searched on your own and just posted your question here right away.

Roblox Anti-Exploit WalkSpeed Script

I am making a game that has different walkspeeds in different sections, but I don't want people to change their own walkspeed with hacks. My solution was to make a part and stretch it to fit the entire area and make it invisible + CanCollide disabled, and then use the child script to kill you if your walkspeed isn't what it should be:
script.Parent.Touched:connect(function(WSChecker)
if not WSChecker.Parent then return end
local humanoid = WSChecker.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
if (humanoid.WalkSpeed ~= 25) then
humanoid.Health = 0
end
end)
Problem is that it does not work with multiple players in the part at one time, and I want to make it so it will kick the player instead of killing them. Is there a way to go about these problems? It has to check their ws only within the part, and I don't know how to make it kick whoever changed their ws instead of killing them.
I would suggest hooking up your function to each player's Humanoid instead, and use the Humanoid.Running event.
Humanoid.Running provides you the speed they're currently running at, which means you can check if that speed is ever above a certain threshold, and punish them if it is.
Code example:
player.Character.Humanoid.Running:Connect(function(Speed)
print(Player, "is running at speed", Speed)
end)
As for kicking, you want to use player:Kick().
if (humanoid.WalkSpeed ~= 25) then
game.Players.LocalPlayer:Kick()
end
that should do the trick...
I think I can help. The solution that worked for me is:
local player = game.Players.LocalPlayer --Make sure it's a local script.
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local hum2 = char.Humanoid
script.Parent = game.StarterPlayer.StarterPlayerScripts
if hum.WalkSpeed >16 then
player:Kick('You have been kicked for possible speed hacks.')
end
if hum2.WalkSpeed >16 then
player:Kick('You have been kicked for possible speed hacks.')
end
if (humanoid.WalkSpeed ~= 25) then
game.localplayer.remove:fire
end
end)
Hopefully, this is a better solution.
You can ofcourse do the above but as an addition to what everyone above said, checking if the walkspeed value is at the desired value is easily bypassable.
Exploiters will get the raw metatable of the game and hook __index to return a normal value for WalkSpeed. Metatables cant be detected either as most modern exploits use C closures instead of Lua. Your best chance is to see how fast the character is moving and teleport them back if player is moving too fast (like a passive anticheat).

Resources