Hello! My script doesn’t work and I need help. In my script, Humanoid.Died doesn’t seem to work but doesn’t give an error. The script looks something like this:
local players = game:GetService("Players")
script.Parent.ClickDetector.MouseClick:Connect(function()
local playersInGame = {}
for i, player in pairs(players:GetPlayers() do
table.insert(playersInGame, player)
player.Character.Humanoid.Died:Connect(function() --here's the line that doesn't work
print(player.Name.. "died")
table.remove(playersInGame, table.find(playersInGame, player)
end)
end
wait(5)
print("round ended")
for i, player in pairs(playersInGame) do
player.leaderstats.Points.Value += 5
end
end)
Any kind of help is appreciated
(sorry if the post was badly formatted, it’s my first time posting)
I have corrected the script (probably) and the below script should work. I have also listed the changes and feedback.
You were missing a parenthesis to close the loop constructor. I also replaced i with _ as you don’t use the i variable and it’s good practice.
The event is firing, just that .Character isn’t there yet so you need to wait using :WaitForChild().
You were also missing another closing parenthesis on the table.remove so I added that.
wait() is deprecated and shouldn’t be used so I went ahead and changed it to task.wait() for you. Thank me later.
local players = game:GetService("Players")
script.Parent.MouseClick:Connect(function()
local playersInGame = {}
for _, player in pairs(players:GetPlayers()) do -- Added parenthesis and substituted i
table.insert(playersInGame, player)
player: WaitForChild("Character").Humanoid.Died:Connect(function() -- Wait for the character.
print(player.Name.. "died")
table.remove(playersInGame, table.find(playersInGame, player)) -- Another parenthesis missing
end)
end
task.wait(5) -- wait() is deprecated
print("round ended")
for _, player in pairs(playersInGame) do -- No need for i
player.leaderstats.Points.Value += 5
end
end)
This script might work but I’m not sure and haven’t tested it. I just wrote this from the back of my head.
local players = game:GetService("Players")
script.Parent.ClickDetector.MouseClick:Connect(function()
local playersInGame = {}
for _, player in pairs(players:GetPlayers()) do
table.insert(playersInGame, player)
player.Character.Humanoid.Died:Connect(function() --here's the line that doesn't work
print(`{player} died`)
table.remove(playersInGame, table.find(playersInGame, player))
end)
end
task.wait(5)
print("round ended")
for _, player in pairs(playersInGame) do
player.leaderstats.Points.Value += 5
end
end)
The problem was actually because in the actual script, there was a line that disables the script which prevented the event to happen. I appreciate all the help. Sorry for wasting your time if I did.