I have this script which changes some leaderstats when the player dies, but for some reason, it only runs after the second time the player dies. Does anyone know why?
local players = game:GetService("Players")
local difficulty = game.Workspace.Difficulty
players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
if leaderstats.Survived.Value >= 60 and difficulty.Value > leaderstats.Difficulty.Value then
leaderstats.Difficulty.Value = difficulty.Value - 1
end
if leaderstats.Record.Value < leaderstats.Survived.Value then
leaderstats.Record.Value = leaderstats.Survived.Value
end
leaderstats.Survived.Value = 0
end)
end)
end)
Sorry if the code is a bit messy. I'm fairly new to coding.

2 comments
It will also run after I have changed the difficulty while in-game.
Update: It works normally in regular servers, but not in studio.