Datastore Script wont save my data

I made a local script where players can change those value u see down here
but when I leave (value = 1 before I leave) and join again the value = 0 (default value)
I don’t know where the problem at in my script

local DataStore = game:GetService("DataStoreService")

-- Py Game Data --

local lvlDT = DataStore:GetDataStore("lvl")
local ExpDT = DataStore:GetDataStore("Exp")


-- Py Settings Data --

local ShadowDT = DataStore:GetDataStore("Shadow")
local BloomDT = DataStore:GetDataStore("Bloom")



game.Players.PlayerAdded:Connect(function(Player)
	local UserId = Player.UserId
	
	local PyGameFolder = Instance.new("Folder",Player)
	PyGameFolder.Name = "PyGameData"
	
	local PySettingsFolder = Instance.new("Folder",Player)
	PySettingsFolder.Name = "PySettingsData"
	
	
	local lvl = Instance.new("IntValue",PyGameFolder)
	lvl.Name = "lvl"
	lvl.Value = lvlDT:GetAsync(UserId) or 1
	
	local Exp = Instance.new("IntValue",PyGameFolder)
	Exp.Name = "Exp"
	Exp.Value = ExpDT:GetAsync(UserId) or 0
	
	
	Exp.Changed:Connect(function()
		local ExpPlus = 0
		if Exp.Value >= 700*lvl.Value then
			ExpPlus = Exp.Value - 700
			Exp.Value = 0 + ExpPlus
			lvl.Value = lvl.Value + 1
		end
	end)
	
	
	--     Settings     --
	
	
	local Shadow = Instance.new("IntValue",PySettingsFolder)
	Shadow.Name = "Shadow"
	Shadow.Value = ShadowDT:GetAsync(UserId) or 0
	
	local Bloom = Instance.new("IntValue",PySettingsFolder)
	Bloom.Name = "Bloom"
	Bloom.Value = BloomDT:GetAsync(UserId) or 0
	
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local UserId = Player.UserId

	local PyGameFolder = Player:WaitForChild("PyGameData")
	local PySettingsFolder = Player:WaitForChild("PySettingsData")

	local lvl = PyGameFolder:WaitForChild("lvl")
	local Exp = PyGameFolder:WaitForChild("Exp")

	local Shadow = PySettingsFolder:WaitForChild("Shadow")
	local Bloom = PySettingsFolder:WaitForChild("Bloom")

	lvlDT:SetAsync(UserId, lvl.Value)
	ExpDT:SetAsync(UserId, Exp.Value)

	ShadowDT:SetAsync(UserId, Shadow.Value)
	BloomDT:SetAsync(UserId, Bloom.Value)
	
end)

As you are changing them in the local script only, it is not replicated to server (hence why it isn’t detecting changes)

Because of this, I recommend you use Remote Events to change the values

1 Like

this is most likely only happening for the last person who is leaving your game

so when the last person on the server leaves the game the server starts to shut down and because your trying to save to the datastore as the server is shutting down its not saving

so whats the solution

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

also i notice your using 4 datastores for 1 player if you read this article

you will notice there are limits to how often you can use the datastore

so its best to save all the player data in 1 key using a table so that you don’t waste your limits

here is some demo code that shows how to save all player data using 1 key in the datastore also it shows how to use bindtoclose to stop the server from shutting down so it will give you enough time to save the last players data who is leaving the game

local playerData = {}

game.Players.PlayerAdded:Connect(function(Player)
    local success, value = pcall(dataStore.GetAsync, dataStore, player.UserId)
    if success == false then return end
    playerData[player] = value or {}
end)

game.Players.PlayerRemoving:Connect(function(Player)
    if playerData[player] == nil then return end
    local success, value = pcall(dataStore.SetAsync, dataStore, player.UserId, playerData[player])
    playerData[player] = nil
end)

-- keep the server running for upto 30 second until playerData table is empty
game:BindToClose(function()
    while next(playerData) ~= nil do
        task.wait()
    end
end)

local function Set(player, key, value)
    if playerData[player] == nil then return end
    playerData[player][key] = value
end

local function Get(player, key)
    if playerData[player] == nil then return end
    return playerData[player][key]
end

Set(player, "lvl", 69)
Set(player, "exp", 420)
1 Like