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)
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
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
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)