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

Datastore variables not saving?

local dataStoreService = game:GetService("DataStoreService")
local yuanData = dataStoreService:GetDataStore("YuanData")
local killsData = dataStoreService:GetDataStore("KillsData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("IntValue", plr)
	leaderstats.Name = "Stats"
	
	local yuan = Instance.new("IntValue", leaderstats)
	yuan.Name = "Yuan"
	yuan.Value = yuanData:GetAsync(plr.UserId) or 0
	
	local kills = Instance.new("IntValue", leaderstats)
	kills.Name = "Kills"
	kills.Value = killsData:GetAsync(plr.UserId) or 100
	
	local element = Instance.new("StringValue", leaderstats)
	element.Name = "Element"
	element.Value = "None"
	
	local gp = Instance.new("IntValue", plr)
	gp.Name = "Gamepasses"
	
	local bFire = Instance.new("BoolValue", gp)
	bFire.Name = "BlueFire"
	bFire.Value = false
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local yuan = plr.Stats.Yuan
	yuanData:SetAsync(plr.UserId,yuan.Value)
end)

so right now I want to only save the "Yuan" variable and whenever I change it in player stats it doesn't save and idk why.

1 answer

Hello, I don’t see anything wrong with your script, but I know many people have this problem when they’re trying to save their data. Check out my answer to another person’s question for the full explanation. Basically, the way to solve this is to use :BindToClose. It will allow your remaining functions which you bound, to run before the server shuts down when the last player leaves or when you’re playing solo in the Studio.

local dataStoreService = game:GetService("DataStoreService")
local yuanData = dataStoreService:GetDataStore("YuanData")
local killsData = dataStoreService:GetDataStore("KillsData")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("IntValue", plr)
    leaderstats.Name = "Stats"

    local yuan = Instance.new("IntValue", leaderstats)
    yuan.Name = "Yuan"
    yuan.Value = yuanData:GetAsync(plr.UserId) or 0

    local kills = Instance.new("IntValue", leaderstats)
    kills.Name = "Kills"
    kills.Value = killsData:GetAsync(plr.UserId) or 100

    local element = Instance.new("StringValue", leaderstats)
    element.Name = "Element"
    element.Value = "None"

    local gp = Instance.new("IntValue", plr)
    gp.Name = "Gamepasses"

    local bFire = Instance.new("BoolValue", gp)
    bFire.Name = "BlueFire"
    bFire.Value = false
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local yuan = plr.Stats.Yuan
    yuanData:SetAsync(plr.UserId,yuan.Value)
end)

game:BindToClose(function()
    for _, plr in pairs(game.Players:GetPlayers()) do
        local yuan = plr.Stats.Yuan
        yuanData:SetAsync(plr.UserId,yuan.Value)
        end
end)

3 comments

0

when the server shuts down? what about if the player leaves and joins another server but the previous server didn't shut down, will the data still be saved??

0

tested it and it still doesn't work

0

If the player isn't the last player in the server and they leave, they are fine.

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