Data Store Script Help

Hey guys! I am a beginner scripter, and kind of used help from YouTube to help me with this script. So if I did anything wrong, I probably had no idea what I was doing. I am willing to learn a lot and improve, so all suggestions are appreciated.

So, I am trying to create a data store save. Basically, once someone redeems a code through a GUI, they player will receive coins. 500 to be exact. That works fine, however, when I leave the game and rejoin, the 500 coins are gone. I have tried many scripts, I have tried going straight on my own, and so far nothing is working. Any ideas on how to improve this script?

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")


local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency



game.Players.PlayerAdded:Connect((function(player)
	
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_")..player.UserId
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Coins"
	money.Parent = leaderstats
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data.")
	end
end))

local function save(player)
	local success, err = pcall (function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then 
		print("Saved Data")
	else
		print("Failed to save data")
	end
end

local function autosave()
	while wait(10) do
		for i, player in pairs (game:GetService("Players"):GetPlayers())
		do	save(player)
		end
	end
		
end

remote.OnServerEvent:Conntect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

function autosave()
	
end
1 Like

So you want to make a datastore save system right? Here is a script that i used in many of my projects it have auto save everytime when the value increase or decrease, datastore will save it.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("MoneySaveSystem")

game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Coins"
	Cash.Value = 0
	Cash.Parent = leaderstats

	Cash.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, Cash.Value)
	
	Cash.Changed:connect(function()
		ds1:SetAsync(player.UserId, Cash.Value)
end)
	
end)

My first time replying i hope this worked for you :slightly_smiling_face:

2 Likes

It does not seem to work unfortunately. I am getting this error.

[02:37:14.667 - Data store coins was not saved as it was not updated.]

Any ideas?

Is Allow API turned on? It is required for datastores.

1 Like

Ye you need to Allow API and publish the game otherwise it wont work

1 Like

Nevermind! I fixed it. I accidentally had a duplicate of the same script which caused it to not work. Thank you so much!