1 | function onTouched(hit) |
2 | if hit.Parent:findFirstChild( "Humanoid" ) and hit.Parent:findFirstChild( "Respawn" ) = nil then |
3 | hit.Parent.Humanoid:TakeDamage(. 5 ) |
4 | end |
5 | end |
6 |
7 | 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?
: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.
1 | function onTouched(hit) |
2 | if hit.Parent:findFirstChild( "Humanoid" ) and hit.Parent:findFirstChild( "Respawn" ) = nil then |
3 | if not game.Players:GetPlayerFromCharacter(hit.Parent) then |
4 | hit.Parent.Humanoid:TakeDamage(. 5 ) |
5 | end |
6 | end |
7 | end |
8 |
9 | script.Parent.Touched:connect(onTouched) |
Easy solution all you have to do is check if they are in the Players service.
1 | function onTouched(hit) |
2 | if hit.Parent:findFirstChild( "Humanoid" ) and hit.Parent:findFirstChild( "Respawn" ) = nil then |
3 | if game.Players:FindFirstChild(hit.Parent.Name) then |
4 | hit.Parent.Humanoid:TakeDamage(. 5 ) |
5 | end |
6 | end |
7 | end |
8 |
9 | script.Parent.Touched:connect(onTouched) |
Good luck! Remember to accept if this works