DateStorage Script Help

Hi, I’m watching a scripting tutorial and my script isn’t working and I can’t find out what the error is. Here is the script and try to tell what’s wrong with it;

local DataStorageService = game:GetService(“DataStoreService”)
local myDataStore = DataStorageService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local playerUserId = "Player_"..player.UserId
--Load Data

local data
local succes, errormessage = pcall(function()
     data = myDataStore:GetAsync(playerUserId)
end)

if succes then
	Cash.Value = data
	 --Setdata to the current cash
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Cash.Value

local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print("Data successfully saved!")
else
	print("error im gonna cry")
	warn(errormessage)
end

end)

1 Like

Do you have “Enable Studio Access to API Services” enabled in the game settings?

Also, you can add at the end of the code:

game:BindToClose(function()
    wait(5)
end)

Just in-case the game ends before the data saves.

1 Like

I don’t think I do how do I do that?

1 Like


You should see this button in the “HOME” tab.

Once you find that button, go to the Security section, and you will find the “Enable Studio Access to API Services” option there.

I just did it and turned it on but it still doesn’t work for some reason

1 Like

Does it say anything in the output when you run the game? You can find the output in the view tab.
image

You made a mistake here, I just realized. There’s supposed to be 2 dots, not three.

Also, if it doesn’t work, and there’s no errors in the output. try adding this to the end of your script:

game:BindToClose(function()
    wait(5)
end)

It stalls the game and gives the data more time to save.

I tried it and it works now! Thank you and also there was 2 dots in my script but for some reason but three in the forum somehow

1 Like

There’s also a way to store lots of data in one key, instead of using like 500 keys. If you want to store lots of data at once, you can use Tables | Roblox Creator Documentation. There are also more ways that are more efficient, that you can search for, but this is just to give you a good start.