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)
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.
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?