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

The DataStore in my script is not saveing why?

Hi. am trying to use Data Store for the first time. Am having a hard time figuring out why this script dos not save my leaderstats when leaving a local test.. This script is not in a local script and I've unchecked the API box in game configuration. Any help would be appreciated :).

Da code:

local DS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DSStats = DS:GetDataStore("Stats")
--local datastore = DS:GetDataStore("PlayerStats")

game.Players.PlayerAdded:connect(function(player)
	--player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
	--wait(2) -- just in case
	
	--creating leaderstats
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local goldstats = Instance.new("IntValue",leaderstats)
	goldstats.Name = "Gold"
	------------------------
	

	local data = DSStats:GetAsync(tostring(player.UserId))
	if data==nil then
		print("data is nil")
		return
	end
	---exstract data
	local PlayerStats = player:FindFirstChild("leaderstats"):GetChildren()
	for i=1,#PlayerStats do
		PlayerStats[i].Value=data[PlayerStats[i].Name] or 0
		print(PlayerStats[i])
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local Data={}
	local PlayerStats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #PlayerStats do -- creates a loop for all the stats
		Data[PlayerStats[i].Name]=PlayerStats[i].Value
		print(PlayerStats[i].Value)
	end
	DSStats:SetAsync(tostring(player.UserId), Data)
end)

(script is in ServerScriptService)

1 answer

✓ Accepted answer

MAKE SURE STUDIO API ACCESS IS ENABLED

There's nothing wrong with your script, just make sure you test it either ingame or by starting up a local server in studio, this is due to the game closing too fast in studio for the datastore to save.

I'd also suggest replacing

tostring(player.UserId)

To either tonumber(player.UserId) or just player.UserId since the userid is a number it'll most likely get niled or just be 0.

2 comments

0

O ok i have only tested it in local test. Ill test it in server test when i get home. Thank you for your knowledge :)

0

worked thanks!

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