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

Datastore not working properly?

I'm not the most experienced with data store, so something was bound to be wrong, but then when i had issues i looked at a datastore tutorial to find my mistakes () and it seemed like nothing was wrong. Here is the script:

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local Level = Instance.new("IntValue",stats)
	
	Level.Name = "Level"
	
	Level.Value = 0
	
	
	local Tix = Instance.new("IntValue",stats)
	
	Tix.Name = "Tix"
	
	Tix.Value = 0
	
	print("Hello , "..plr.Name)
	local UsrId = "Player_".. plr.UserId
	
	local data
	local success, errormsg = pcall(function()
		local data = DataStore:GetAsync(UsrId)
	end)
	
	if success then
		Tix.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local UsrId = "Player_".. plr.UserId
	
	local data = plr.leaderstats.Tix.Value
	
	local success, errormsg = pcall(function()
		DataStore:SetAsync(UsrId, data)
	end)
	if success then
		print("Data saved")
	else
		print("Data not saved")
		warn(errormsg)
	end

end)

1 answer

Well you didn’t specify you’re Problem so I’ll just assume it doesn’t save properly. This is because 1: You tested it on studio (probably) and 2 If you want it to save when you’re leaving as the last person, you should use BindToClose. This will keep the server up for an additional max of 30 secs or shorter depending on how long your function takes to complete.

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