so I have a localscript and i want something to be removed when the player dies and at first it works because when the player dies for the first time then the item is removed but after they respawn and get the item again and die again the item isnt removed and is still there.
local player = game.Players.LocalPlayer
local Humanoid = player.CharacterAdded:Wait():WaitForChild("Humanoid")
local alive = true
local function Dead()
alive = false
print("died")
end
function spawned()
alive = true
print("alive")
end
player.CharacterAdded:Connect(spawned)
Humanoid.Died:Connect(Dead)
i dont whats wrong with what I am doing and i dont know another way of doing this and i cant find any solution.
another thing
if the character gets defined before you make the variable before it, it will wait forever when you use :Wait() for the character that already spawned in to load. You need to do this:
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
When the Humanoid dies that reference you have to the Humanoid becomes nil because remember that every time a player dies a new character model is created and thus a new humanoid as well. What I recommend you do is to put the local script in starter character scripts because whenever the character dies the script will get parented to character again, thus perfect for your situation. Here is the code:
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
--Give Item here
Humanoid.Died:Connect(function()
--Remove Item here
end)
Thanks so much for the solution I didn’t even know what startcharacterscripts was for and thats actually really useful. I am making an fps game and I am a newbie to roblox scripting and all of its features so choosing to create an FPS from scratch is a challenge. It’s a big relief for me that i easily found a solution from here instead of searching for awhile on the forums and on the internet and I noticed a new character was created when respawning and I didn’t know how to reference a variable for that but this really helped thanks a lot.