local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinsSaveSystem")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Gold = Instance.new("IntValue",leader)
Gold.Name = "Coins"
Gold.Value = ds:GetAsync(player.UserId) or 0
ds:SetAsync(player.UserId, Gold.Value)
Gold.Changed:connect(function()
ds:SetAsync(player.UserId, Gold.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
Script for the objects giving money:
function CoalOre()
local Ore = Instance.new("Part",workspace)
Ore.Anchored = true
Ore.CFrame = CFrame.new(math.random(-50, 50), 1, math.random(-50, 50))
Ore.Shape = Enum.PartType.Ball
Ore.TopSurface = Enum.SurfaceType.Smooth
Ore.BottomSurface= Enum.SurfaceType.Smooth
Ore.Size = Vector3.new(3, 3, 3)
Ore.BrickColor = BrickColor.new("Black")
Ore.Material= Enum.Material.Pebble
Ore.Touched:connect(function(p)
if p.Parent.Parent.Name == "Coal Pickaxe" or p.Parent.Parent.Name == "Copper Pickaxe" or p.Parent.Parent.Name == "Bronze Pickaxe" or p.Parent.Parent.Name == "Iron Pickaxe" or p.Parent.Parent.Name == "FireStone Pickaxe" or p.Parent.Parent.Name == "IceCrystal Pickaxe" then
Ore:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then Coins.Value = Coins.Value + 2
end
end
end)
end
while wait(math.random(2,5)) do
CoalOre()
end
Your issue is that PlayerRemoving doesn’t fire in local studio test (because the server closes before it can actually fire it). What you should be doing is using game:BindToClose() which will run a function before the server itself closes. One of the most common usecases is to save all user data before closing the server
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
pcall(saveData, v)
end
end)
ps Roblox recommend having a check to make sure it’s not studio (RunService:IsStudio) otherwise you’ll have to wait for BindToClose() to finish before returning to edit mode
Please avoid using DS2, it adds unnecessary redundancies to your code, as well not being able to use UpdateAsync, a fairly useful function inside vanilla datastores which allows you to update your values without losing the original value
There’s no way for me to check if this would actually work, because, the Gold/Coins value won’t change with this:
function CoalOre()
local Ore = Instance.new("Part",workspace)
Ore.Anchored = true
Ore.CFrame = CFrame.new(math.random(-50, 50), 1, math.random(-50, 50))
Ore.Shape = Enum.PartType.Ball
Ore.TopSurface = Enum.SurfaceType.Smooth
Ore.BottomSurface= Enum.SurfaceType.Smooth
Ore.Size = Vector3.new(3, 3, 3)
Ore.BrickColor = BrickColor.new("Black")
Ore.Material= Enum.Material.Pebble
Ore.Touched:connect(function(p)
if p.Parent.Parent.Name == "Coal Pickaxe" or p.Parent.Parent.Name == "Copper Pickaxe" or p.Parent.Parent.Name == "Bronze Pickaxe" or p.Parent.Parent.Name == "Iron Pickaxe" or p.Parent.Parent.Name == "FireStone Pickaxe" or p.Parent.Parent.Name == "IceCrystal Pickaxe" then
Ore:Destroy()
local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Gold")
if Coins then Coins.Value = Coins.Value + 2
end
end
end)
end
while wait(math.random(2,5)) do
CoalOre()
end
DataStore2 has a time stamp where if you save data it will save that stamp so if the player`s data is lost it will make it revert to the previous time stamp
That script was working back when I had the leaderstat script as
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Money = Instance.new("IntValue")
Money.Name = "Gold"
Money.Value = 0
Money.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Money = Instance.new("IntValue")
Money.Name = "Gold"
Money.Value = 0
Money.Parent = leaderstats
end
local function onPlayerLeave(player)
Money.Value = player.Money
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerLeave)