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

Why doesnt the script run when I die? (attempt to call a nil value)

I have a localscript that is inside a player's character. When the player's character dies its supposed to print("Died"), unequip the tool it is holding and fire a remote event. However what happens is that when the character dies, it says attempt to call a nil value. Could someone tell me why it says this?

local plr = game.Players.LocalPlayer
local char = plr.Character
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Attack = ReplicatedStorage:WaitForChild("StarterToolsOnDeath")



if char.Humanoid.Died:Connect() then
	print("Died")
	game.ReplicatedStorage:FindFirstChild("StarterToolsOnDeath"):FireServer(char)
	char.Humanoid:UnequipTools()
end

2 comments

0

the error simply means the function does not have an argument. in this case, you didn't connect your function.

0

either do what @EnzoTDZ_YT said in the answer, or remove the :connect() in line 8

1 answer

✓ Accepted answer

You appear to checking for its connection instead you want to make a function to run when the Died event is ran.

local plr = game.Players.LocalPlayer
local char = plr.Character
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Attack = ReplicatedStorage:WaitForChild("StarterToolsOnDeath")

char:WaitForChild("Humanoid").Died:Connect(function() -- Used WaitForChild("") to hook onto humanoid and made the connect call a function as it doesn't return a bool value.
    print("Died")
    game.ReplicatedStorage:FindFirstChild("StarterToolsOnDeath"):FireServer(char)
    char.Humanoid:UnequipTools()
end)

3 comments

0

Thanks!

0

You're welcome!

0

the script is right, but the answer is wrong. he connected it without an argument.

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