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

When a player dies their stage number gets reset to 0. How to fix?

Asked by 2 years ago

When a player dies in my obby their stage leaderstat gets reset to 0. They still respawn at the right checkpoint, but their stage number visually says 0 in the leaderboard until they reach the next checkpoint. I have pasted every script I have relating to the checkpoint system; some might not be necessary.

Leaderstats script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local ls = Instance.new("Folder", player)
        ls.Name = "leaderstats"

        local stage = Instance.new("NumberValue", ls)
        stage.Value = 0
        stage.Name = "Stage"

        local stageJoined = player.leaderstats.stage.Value

        wait(0.01)

        char.HumanoidRootPart.Position = game.workspace.Stages2:FindFirstChild("Stage"..stageJoined).Position
    end)
end)

Checkpoints script in ServerScriptService:

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(player)
    local stage = player:WaitForChild("leaderstats"):WaitForChild('Stage')
    player.CharacterAdded:Connect(function(character)
        local checkpoint = game.Workspace.Stages:FindFirstChild(stage.Value)
        delay(.01, function() 
            character:SetPrimaryPartCFrame(checkpoint.CFrame + Vector3.new(0,10,0))
        end)
    end)
end)

CheckpointManager Script in the "Stages" folder:

for i,v in pairs(script.Parent:GetChildren()) do
        if v:isA("Part") then
            v.Touched:Connect(function(hit)
            local character = hit.Parent
            local player = game.Players:GetPlayerFromCharacter(character)
            if player then
                        local touchedstage = tonumber(v.Name)
                if touchedstage == (player.leaderstats.Stage.Value + 1) then
                    player.leaderstats.Stage.Value = touchedstage
                end
                    end
            end)
        end
end

Datastore script in ServerScriptService:

local DSService = game:GetService('DataStoreService'):GetDataStore('PUTRANDOMCHARACTERSHERE')
game.Players.PlayerAdded:connect(function(plr)

    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue', plr)
    local savevalue = Instance.new('IntValue')
    leaderstats.Name = 'leaderstats'
    savevalue.Parent = leaderstats
    savevalue.Name = 'Stage'

    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        DSService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Stage.Value}
DSService:SetAsync(uniquekey, Savetable)
end)
0
Not related to the question, but try seeing if you're able to merge some of your scripts. Just a tip for tryna stop script clutter. Also, you don't need to make a variable for their unique key! You can simply use their user id and that will be good. Antelear 185 — 2y
0
Thats probably a good idea thanks BunjiBloxxer 51 — 2y

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago

at leaderStats scripts line 2 it states that when the player respawns you create a new leaderstats https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded "The CharacterAdded event fires when a player’s character spawns (or respawns)" also it might be better for performance to not use instance.new() for parenting

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local ls = Instance.new("Folder", player)
        ls.Name = "leaderstats"

        local stage = Instance.new("NumberValue", ls)
        stage.Value = 0
        stage.Name = "Stage"

        local stageJoined = player.leaderstats.stage.Value

        wait(0.01)

        char.HumanoidRootPart.Position = game.workspace.Stages2:FindFirstChild("Stage"..stageJoined).Position
    end)
end)
0
Ty! I don't know why I had that in there. I changed Instance.new too BunjiBloxxer 51 — 2y
Ad

Answer this question