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

Why does my script only run after the second time the player dies?

Asked by
AronYstad 101
3 years ago

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.

0
It will also run after I have changed the difficulty while in-game. AronYstad 101 — 3y
0
Update: It works normally in regular servers, but not in studio. AronYstad 101 — 3y

Answer this question