I have set up a datastore that is working properly when i change a player’s Cash on the server but when i make a script that adds data and saves when a player leaves, it simply does not work. Any help?
Here is the code i used:
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild(“leaderstats”)
local money = leaderstats:WaitForChild(“Money”)
money.Value = money.Value +10000000000
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
local DataStore = game:GetService(“DataStoreService”)
local d = DataStore:GetDataStore(v.Name)
d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
end
end)
i also have a main saving script, which i know works but it still happens with that. Here is the main one:
game.Players.PlayerAdded:connect(function(player)
local DataStore = game:GetService(“DataStoreService”)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = “leaderstats”
for i,v in pairs(script:GetChildren()) do
local d = DataStore:GetDataStore(v.Name)
local x = Instance.new(“NumberValue”,leaderstats)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(script:GetChildren()) do
local DataStore = game:GetService(“DataStoreService”)
local d = DataStore:GetDataStore(v.Name)
d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
end
end)
I can’t really understand your code because they’re everywhere / unorganised, but I see that you added the player’s money in a local script
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild(“leaderstats”)
local money = leaderstats:WaitForChild(“Money”)
money.Value = money.Value +10000000000
end)
Don’t add money / leaderstats on the client do it on the server
I’ve quickly written some code for you to use.
I’ve also added some notes to help explain some things.
I hope you can use this source code and research more into data stores and remotes to gain a better understanding on the topic.
Tell me if there is any errors as I wrote this quick.
--Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("Money")
local AddMoneyRemote = ReplicatedStorage:WaitForChild("AddMoney")
local CreateLeaderstats = function(Player, Data)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = Leaderstats
Money.Value = Data.Money
end
game.Players.PlayerAdded:Connect(function(Player)
local Success, Data = pcall(function() --Use pcall for no data loss, error provention, etc
return Datastore:GetAsync(Player.UserId)
end)
if Success then
if Data then
CreateLeaderstats(Player, Data)
else
local Data = {
["Money"] = 0
}
CreateLeaderstats(Player, Data)
end
else
--Do error stuff.
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Data = {
["Money"] = Player.leaderstats.Money.Value
}
Datastore:SetAsync(Player.UserId, Data)
end)
AddMoneyRemote.OnServerEvent:Connect(function(Player)
--Im guessing this is a admin script because I don't see any practical reason to add so much money so
--I've added a sanity check to add money.
if Player.Name == "KieranKreates" then
Player.leaderstats.Money = Player.leaderstats.Money +1000000000000
end
end)
--Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AddMoneyRemote = ReplicatedStorage:WaitForChild("AddMoney")
AddMoneyRemote:FireServer() --Fire this whenever you want to add money.
Wait, im confused. When i use a MouseButton1Click event and fire the server, it does not work. This creates the leaderstats and everything but it does not add the money. Any help?