I'm working on a game that involves DataStore. My code works in studio, and saves even when I join an actual server but doesn't work once in game.

local exp = game:GetService("DataStoreService"):GetDataStore("exp") local lvl = game:GetService("DataStoreService"):GetDataStore("level")

game.Players.PlayerAdded:connect(function(Player)
    	local PlayerKey = "user_" .. Player.userId
 
   	local leaderstats = Instance.new("IntValue", Player)
   	leaderstats.Name = "leaderstats"
 
    	local experience = Instance.new("IntValue", leaderstats)
    	experience.Name = "Experience Points"
    	experience.Value = exp:GetAsync(PlayerKey)

	local level = Instance.new("IntValue", leaderstats)
    	level.Name = "Level"
    	level.Value = lvl:GetAsync(PlayerKey)

	game.StarterGui.IntValues.exp.Value = experience.Value
	game.StarterGui.IntValues.level.Value = level.Value

	spawn(function() 
		game.StarterGui.IntValues.exp:GetPropertyChangedSignal("Value"):connect(function()
			exp:SetAsync(PlayerKey, game.StarterGui.IntValues.exp.Value)
			experience.Value = game.StarterGui.IntValues.exp.Value
			lvl:SetAsync(PlayerKey, game.StarterGui.IntValues.level.Value)
			level.Value = game.StarterGui.IntValues.level.Value
			wait()
		end)
	end)
end)

3 comments

0

On line 9 & 13, I suggest you putting "or 0" if a player is just starting out.

0

On line 20 & 22, I also suggest you doing ":UpdateAsync()" http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/UpdateAsync

0

Thanks for the reponse, but it didnt work :(