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