Datastore Error

Hello.

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)

Putting all of that together, i do not know.

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

1 Like

I wouldn’t use the players name as a data store. That will create multiple stores when really you only need one for everyone’s data

How do i do it on the server because i tried adding the money on a script but it wouldn’t add any money?

Use a RemoteEvent. Fire it to the server from the client.

Example code:

LocalScript:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent:FireServer()

Script:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function()
    -- insert whatever code here
end)

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.
2 Likes

Thank you so much! This really helped.

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?

Button.MouseButton1Click:Connect(function()
    AddMoneyRemote:FireServer()
end)

ok so i have just added this, is this correct
local Button = script.Parent

local AddMoneyRemote = game.ReplicatedStorage.AddMoney

Button.MouseButton1Click:Connect(function()

AddMoneyRemote:FireServer()

end)

Yes what you have provided is correct, any more issues?

yeah, i used every script the same way you typed them and it is not adding any money to my leaderstats.

Are you trying to add money to the person who fired the remote?

Yes. I am trying to add money but it will not. This money goes to anyone who clicks not just me.

Did you test this in your studio because i did everything as you said again just now and it didnt work.