Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make sure thats its an npc not a player?

Asked by 3 years ago
Edited 3 years ago
function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") and hit.Parent:findFirstChild("Respawn")  = nil then
        hit.Parent.Humanoid:TakeDamage(.5)
    end
end

script.Parent.Touched:connect(onTouched)

How would I make sure thats its a npc? Well, the player doesn't have a script named "Respawn" in it, but my npc does. so how would I use the :findFirstChild() function, but make sure that it respawns as nil, not a actually find it. Is there an other function?

2 answers

Log in to vote
2
Answered by 3 years ago

:GetPlayerFromCharacter is a great way to see if a character is a player, in your case, we can just do the opposite by checking if it didn't return the player or true.

I'm using another if statement just to make the line not as long and make it less confusing.

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") and hit.Parent:findFirstChild("Respawn")  = nil then
    if not game.Players:GetPlayerFromCharacter(hit.Parent) then
       hit.Parent.Humanoid:TakeDamage(.5)
    end
    end
end

script.Parent.Touched:connect(onTouched)
1
line 2 is an error. `if respawn = nil then` what's wrong with this firewolf? Not to mention, `:findFirstChild()` is deprecated, and `:FindFirstChild()` should be used instead. same goes for `:connect()`, it should be `:Connect()` Gey4Jesus69 2705 — 3y
0
Make sure to have double equal to sign. BestCreativeBoy 1395 — 3y
0
i just copied and pasted his script, and then just edited it. FirewolfYT_751 223 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Easy solution all you have to do is check if they are in the Players service.

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") and hit.Parent:findFirstChild("Respawn")  = nil then
    if game.Players:FindFirstChild(hit.Parent.Name) then
           hit.Parent.Humanoid:TakeDamage(.5)
    end
    end
end

script.Parent.Touched:connect(onTouched)

Good luck! Remember to accept if this works

0
sorry, didnt see this untill right now, it does work. Spiyder1 81 — 3y
0
sorry, didnt see this untill right now, it does work. Spiyder1 81 — 3y

Answer this question