New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Value of my IntValue object changes but when I reference it in a scrip it still shows 0?

Asked by 14 days ago
Edited 14 days ago

I put strength, intelligence, defense, and health stat values in a folder in the player call playerstats and in another local script when a player levels up and adds points to that stat its suppose to go up which it does when I check the value, but when I try to save it using a script in serverscriptservice the values show up as 0.

local function savePointAllocation(player)
    local playerstats = player.PlayerStats
    local str = playerstats:FindFirstChild("Strength")
    local def = playerstats:FindFirstChild("Defense")
    local int = playerstats:FindFirstChild("Intelligence")
    local hp = playerstats:FindFirstChild("Health")
    local success, err = pcall (function()
        local newStatValues = {
                Strength = str.Value,
                Defense = def.Value,
                Intelligence = int.Value,
                Health = hp.Value 
        }
        plrStats:SetAsync(player.UserId.."Stats",newStatValues) 
    end)

    if success then
        print("Point allocation successfully saved!")
    elseif not success then
        repeat
            print(err)
            local newStatValues = {
                Strength = str.Value,
                Defense = def.Value,
                Intelligence = int.Value,
                Health = hp.Value 
            }
            plrStats:SetAsync(player.UserId.."Stats",newStatValues)
        until success
    end
end
game.Players.PlayerRemoving:Connect(function(player)
    savePointAllocation(player)
end)
0
Is this a localscript? ProqrammedGreen 249 — 14d
0
its a script inside the serverscripservice Fangblade16 11 — 14d

1 answer

Log in to vote
0
Answered by 14 days ago
Edited 14 days ago

The classic you forgot game:BindToClose().

Basically before your datastore can successfully store all data the game server has already shutdown and terminates the datastore process. However if you use game:BindToClose(), loop through all remaining players in the game and save the data for each of the players at that point, then the game will wait until the saving has been handled and then exit the game.

The wiki explains this exact issue aswell:

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

0
the game server isn't shutting down and not saving, it's that when it gets the values from the player stats to save it in a datastore it gets 0 even if the value is not 0 Fangblade16 11 — 14d
Ad

Answer this question