Datastore isnt working

followed a youtube tutorial for a currency script. The datastores are always giving the error message though. Can anyone help me spot the issue?

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




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




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 = "Rubles"
	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:Connect(function(player, amount)
	player.leaderstats.Rubles.Value += amount
end)
spawn(autosave)

Add the warning message to the failed print statement (remember to also do the saved print statement)

print("failed to load data Error: "..err)

Also, it doesn’t seem like you are running the save function

1 Like


also wdym

The save function in the script is defined but it never runs

Do game:GetService(“Players”).PlayerRemoving:Connect(save)

1 Like

just at the end of the script or what

Yeah or you can just do it directly like this:

game:GetService("Players").PlayerRemoving:Connect(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)

By the way, i dont think that will fix the error you are getting. I will look at the original script now.

1 Like

Hello, the reason why it is not working is because you named money as Rubles.

1 Like

Wait, it might be because the Money value is an int value, not a number value

Instead of:

local money = Instance.new("IntValue")

do

local money = Instance.new("NumberValue")

Edit 2: Like @redpepperdoc2006 said,

moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)

needs to be

moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Rubles.Value)
2 Likes

could u highlight where im supposed to replace this at? having some trouble

The first thing you need to replace is right below your playeradded event.

The second one you need to change is this

1 Like

Yeah I did those, and it works now. Thanks so much both of you guys! before I mark as solution, could I maybe ask how to make a script/edit the script that would make it so players get 10 rubles every minute?

Sure, give me a second to write the script. I will edit this post when i am done with it.

EDIT: he beat me to it😭

remember to lower the w in while in his post

1 Like
While true do
 task.wait(60)
 for i, player in pairs(Players:GetPlayers()) do
  player.leaderstats.Rubles.Value += 10
 end
end

Edit: set to 60 seconds cause I read what you wanted wrong lol.

1 Like

thank you so much, both of you! Very helpful :heart: :smile:

1 Like

so just put that at the bottom of the script, or insert that somewhere? sorry im super new to scripting as a whole.

Yes, just remember to add players as a variable (put it at the top of your script)

local Players = game:GetService("Players")

Make sure to put it at the bottom of everything, cause since it’s a loop, it will not let anything below it run.

image
im getting red lines uh oh

like i said in the other post, while needs to be lowercase.(sorry if that sounded rude)

1 Like

put While to lowercase, I wrote it on the fly. :sweat_smile: