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

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

Asked by
das1657 20
3 years ago

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
0
the error simply means the function does not have an argument. in this case, you didn't connect your function. JesseSong 3916 — 3y
0
either do what @EnzoTDZ_YT said in the answer, or remove the :connect() in line 8 JesseSong 3916 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
Thanks! das1657 20 — 3y
0
You're welcome! EnzoTDZ_YT 275 — 3y
0
the script is right, but the answer is wrong. he connected it without an argument. JesseSong 3916 — 3y
Ad

Answer this question