Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why isnt this script saving int values?

ive been sat here at my desk for at least 4 hours trying to figure out whats going wrong. In my output panel i can see that its printing success but thats it. I put the script into ServerScriptService and ive tested it from within studio and from the roblox website

local dataStoreService = game:GetService("DataStoreService")
local obbyDataStore = dataStoreService:GetDataStore("obbyDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue",leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    local stage = Instance.new("IntValue",leaderstats)
    stage.Name = "Stage"
    stage.Value = 0

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"
    kills.Value = 0

    local datastagevar

    local success, errormessage = pcall(function()
        datastagevar = obbyDataStore:GetAsync(player.UserId,"-stagevar")
        datastage = obbyDataStore:GetAsync(player.UserId,"-stage")
        datakills = obbyDataStore:GetAsync(player.UserId,"-kills")
        datacoins = obbyDataStore:GetAsync(player.UserId,"-coins")
    end)
    if success then
        if datastagevar then
            player.Team = game.Teams[datastagevar]
        else
            player.Team = game.Teams.Spawn
        end
        coins.Value = datacoins
        kills.Value = datakills
        stage.Value = datastage
		print("success")
    end
end)




game.Players.PlayerRemoving:Connect(function(player)
    local teamName = player.Team.Name 
    local success, errormessage = pcall(function()
        obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
        obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
        obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
        obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
		print("working")
    end)
    if success then
        print ("Data Saved")

    else
        print("Error, i am now sentient human")
        warn(errormessage)
    end


end)


this may be unrelated but im getting these in my output

18:30:14.516 - SliceCenter ((500.000000,112.000000), (501.000000,113.000000)) is outside the bounds of imageOffset ((492.000000,104.000000), (509.000000,112.000000)) for GuiImage[CoreGui.TopBar.TopBarFrame.RightFrame.MoreMenu.MoreMenuContainer.OverlayContextualMenu.PositionFrame.BaseMenu.ClippingFrame.ScrollingFrame.cell 1]
18:30:14.516 - SliceCenter ((500.000000,112.000000), (501.000000,113.000000)) is outside the bounds of imageOffset ((492.000000,104.000000), (509.000000,112.000000)) for GuiImage[CoreGui.TopBar.TopBarFrame.RightFrame.MoreMenu.MoreMenuContainer.OverlayContextualMenu.PositionFrame.BaseMenu.ClippingFrame.ScrollingFrame.cell 1.StateOverlay]

1 answer

✓ Accepted answer

Ok, that other stuff in the output has nothing to do with it, I get that to and so does every roblox player, I think roblox is working on fixing it, but anyway, the issue you are having is that the game is ending before the player leaves the game. This happens when there is only one player in the game. What you can do to fix this is copy your player removing code but replace the function, like so:

local dataStoreService = game:GetService("DataStoreService")
local obbyDataStore = dataStoreService:GetDataStore("obbyDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue",leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    local stage = Instance.new("IntValue",leaderstats)
    stage.Name = "Stage"
    stage.Value = 0

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"
    kills.Value = 0

    local datastagevar

    local success, errormessage = pcall(function()
        datastagevar = obbyDataStore:GetAsync(player.UserId,"-stagevar")
        datastage = obbyDataStore:GetAsync(player.UserId,"-stage")
        datakills = obbyDataStore:GetAsync(player.UserId,"-kills")
        datacoins = obbyDataStore:GetAsync(player.UserId,"-coins")
    end)
    if success then
        if datastagevar then
            player.Team = game.Teams[datastagevar]
        else
            player.Team = game.Teams.Spawn
        end
        coins.Value = datacoins
        kills.Value = datakills
        stage.Value = datastage
        print("success")
    end
end)




game.Players.PlayerRemoving:Connect(function(player)
    local teamName = player.Team.Name 
    local success, errormessage = pcall(function()
        obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
        obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
        obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
        obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
        print("working")
    end)
    if success then
        print ("Data Saved")

    else
        print("Error, i am now sentient human")
        warn(errormessage)
    end


end)

game:BindToClose(function() -- This code will run BEFORE the game closes, in other words the game cannot close until this code has ended
for _, player in pairs(game.Players:GetPlayers()) do -- loops through the players to get the player

    local teamName = player.Team.Name 
    local success, errormessage = pcall(function()
        obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
        obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
        obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
        obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
        print("working")
    end)
    if success then
        print ("Data Saved")

    else
        print("Error, i am now sentient human")
        warn(errormessage)
    end

end
end)

2 comments

0

i never thought that the problem would be that the game is closing before it saves

0

Same, roblox is kind of weird sometimes

This discussion is preserved as it was. New answers, comments, and votes are closed.