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

Data Store not saving/loading?

I keep trying to use data store to save variables (which is fairly new to me) . I run the game, edit the variable, and re-start the game, and the DataStore brings up a value of 0. Here is my current code. Anyone have an idea as to what's wrong? EDIT: I have API in studio on. My guess is something to do with the player removed function at the end

local DataStore = game:GetService("DataStoreService")
local TutorialData = DataStore:GetDataStore("CashStore")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "stats"
	folder.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = folder
	Cash.Value = TutorialData:GetAsync(player.UserId) or 500
	TutorialData:SetAsync(player.UserId, 0)
	
	game.Players.PlayerRemoving:Connect(function(player)
		TutorialData:SetAsync(player.UserId, Cash)
		wait(1)
	end)
end)

4 comments

0

line 13 you basically wipe all the data and set it to 0

0

Oh wow...:P thanks lol

0

Also , Please use 'pcall(function()' when Setting or Getting Data from DataStores. It would be dreadful to see your code error and fail during a game.

0

In line 13 you set the data to 0

 TutorialData:SetAsync(player.UserId, 0)

1 answer

Thanks to everyone :) this was answered by @greatneil80 and a tip by @Tizzel40 Thanks to you two!

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