I have a currency script that should allow players to retain their money after gaining it. but whenever you leave the game and rejoin it doesn’t save the money gained while playing. not really sure what is going wrong here.
--CURRENCY SYSTEM--
local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService") :GetDataStore("AdventureClickerStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder
local ID = currencyName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
currency.Value = savedData
print("Player Loaded")
else
currency.Value = 100
print("New Player")
end
end)
local DataStore = game:GetService("DataStoreService") :GetDataStore("Weapons")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Weapons"
folder.Parent = player
local ID = player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
print("loaded weapons")
end)
--Saving when Player Leaves
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
print("logged out successful")
--when game is ready to shut down
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game shut down")
end
end
task.wait(3)
end)
--PLAYER DAMAGE--
game.Players.PlayerAdded:Connect(function(player)
local damage = Instance.new("IntValue")
damage.Name = "damage"
damage.Parent = player
damage.Value = 1
end)
Thanks for the help!