Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone explain me why this script doesn't work when the player dies and respawns ?

it prints damaged but the player doesnt take any damage, works when u spawn for the first time tho

local players = game:GetService("Players")
local player = players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
local function onTouch(Touch)
 	if Touch.Parent:FindFirstChild("Humanoid") ~= nil and script.Parent.Humanoid.Health > 0 then
	
		
		 hum:TakeDamage(1)
	  
		print("Damaged")
	
	end
end


script.Parent.HumanoidRootPart.Touched:Connect(onTouch)

5 comments

0

remove the ~= nil

0

i already tried to remove the checks but it doesnt work, removed the ~= nil only and still doenst work

0

When you say it works when you spawn the first time do you mean it kills you? Dealdamage doesn’t work when force field is enabled

0

Also don’t use the hum that you get at the top, get the humanoid that you used in the if statement

0

i disabled the force field from the spawn it's weird because it prints damaged the checks passed trough but i dont take damage i tried with hum.Health = hum.Health - 1 didnt work still

3 answers

✓ Accepted answer
local function onTouch(Touch)
    local hum = Touch.Parent:FindFirstChild(“Humanoid”)
    if hum and script.Parent.Humanoid.Health > 0 then


         hum:TakeDamage(1)

        print("Damaged")

    end
end


script.Parent.HumanoidRootPart.Touched:Connect(onTouch

5 comments

0

yes this pretty much fixed it u think that the characterAdded event in general breaks a script when u want it to work all the time ?

0

Well what was happening was it was making the humanoid the new person that was Added. So idk it was weird how you were doing it lol.

0

ohhhh i remember now why i did this because the npcs when they touched another npc they deals damage to them but i want them to deal damage only for the player

0

Then you just Check to see if the player from character inside OnTouch is a player and if it is then youdeal damage. 

0

something like this local character = game.Workspace.Player local player = game.Players:GetPlayerFromCharacter(character) into the touch function right ?

Not sure exactly where this script is inside of your game which may affect the answer, but the issue you are having is likely because of the player's character resetting.

Every time a player respawns Roblox will load a new character for them. That means that you need to redefine your character variables. So your script would look something like this...

local players = game:GetService("Players")
local player = players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid

function onTouch(Touch)
   if Touch.Parent:FindFirstChild("Humanoid") ~= nil and script.Parent.Humanoid.Health > 0 then
        hum:TakeDamage(1)

        print("Damaged")

    end
end
				-- This fires every time the character is added
player.CharacterAdded:Connect(function(newChar) 
				-- redefine your variables to the updated references
	char = newChar
	hum = newChar:WaitForChild("Humanoid")
				-- reconnect the touch event on the new character
	char:WaitForChild("HumanoidRootPart").Touched:Connect(onTouch) 
end)
				-- fired once on the first load of the character
char:WaitForChild("HumanoidRootPart").Touched:Connect(onTouch) 

This fixed everything ty for ur help guys really appreciated

  • Works when u die and respawn
  • Doesn't deal damage to other NPCs but only the player
local players = game:GetService("Players")


function onTouch(Touch)

local player = players:GetPlayerFromCharacter(Touch.Parent)

   
   if Touch.Parent:FindFirstChild("Humanoid") ~= nil and script.Parent.Humanoid.Health > 0 and player then
        Touch.Parent:FindFirstChild("Humanoid"):TakeDamage(1)

        print("Damaged")

    end
end


script.Parent.HumanoidRootPart.Touched:Connect(onTouch)

This discussion is preserved as it was. New answers, comments, and votes are closed.