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

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

Asked by 4 years ago

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)
0
remove the ~= nil valledestroy 54 — 4y
0
i already tried to remove the checks but it doesnt work, removed the ~= nil only and still doenst work EvilTony99 -7 — 4y
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 SethHeinzman 284 — 4y
0
Also don’t use the hum that you get at the top, get the humanoid that you used in the if statement SethHeinzman 284 — 4y
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 EvilTony99 -7 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
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

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 ? EvilTony99 -7 — 4y
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. SethHeinzman 284 — 4y
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 EvilTony99 -7 — 4y
0
Then you just Check to see if the player from character inside OnTouch is a player and if it is then youdeal damage.  SethHeinzman 284 — 4y
0
something like this local character = game.Workspace.Player local player = game.Players:GetPlayerFromCharacter(character) into the touch function right ? EvilTony99 -7 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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) 
Log in to vote
0
Answered by 4 years ago

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)

Answer this question