Hey there im trying to make a currency system when i add money to my player and rejoin it is the same as the starter one
the code will be bellow i have searched youtube the whole day to find a working one there was non please help
local currencyName = “coins”
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“TestDataStore”)
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("Data loaded")
else
-- New player
currency.Value = 5
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName…“-”…player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game:BindToClose(function()
-- When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(5)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
local ID = currencyName…"-"…player.UserId
DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end
end
end)
local currencyName = "coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
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
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData then
currency.Value = savedData
print("Data loaded")
else
-- New player
currency.Value = 5
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end
end
end)
Seems the problem was either you were using the savedData variable as nil and checking whether it was still nil or because you had an unknown pair of quotation marks that didn’t work with Roblox scripting.
Here, the problem this time was the pcall function for some reason lol
local currencyName = "coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
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 = DataStore:GetAsync(ID)
if savedData then
currency.Value = savedData
print("Data loaded")
else
-- New player
currency.Value = 5
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end
end
end)
This is when I start thinking the pcall function is deprecated.