Remove events to update leaderstats datastore

Hello everyone. I’m trying to update my leaderstats which is stored in a datastore. Searching the web has indicated I need a remote event to update my datastore from my local script. I created a simple button that I wanted it to update my leaderstats value. I’m having trouble figuring out how the arguments work for the remove events after reading the api page over and over.

local button = script.Parent.Parent.ClickMe
local player = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("updateMoney")

button.MouseButton1Up:Connect(function()
	print("also clicked")
	local leaderstats = player.leaderstats
	local aureus = leaderstats and leaderstats:FindFirstChild("aureus")
	if aureus then
		aureus.Value = aureus.Value + 10
	end    
end)
button.MouseButton2Up:Connect(function()
	print("clicked")
	local leaderstats = player.leaderstats
	local aureus = leaderstats and leaderstats:FindFirstChild("aureus")
	if aureus then
		aureus.Value = aureus.Value + 10
	end    
end)

remoteEvent:FireServer(game.ReplicatedStorage.updateMoney)

Above is my lua script that I wanted to have update my leaderstats. I tried passing the removeEvent “updatemoney” but I’m obviously doing something terribly wrong.

local dataStoreService = game:GetService("DataStoreService") -- grabbing datastore service
local dataStore1 = dataStoreService:GetDataStore("DS1") -- setting local var to datastore 
local replicatedStorage = game:GetService("ReplicatedStorage") -- grabs RS
local remoteEvent = replicatedStorage:WaitForChild("updateMoney") -- grabs Event

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder") -- creates our leaderboard
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	local aureus = Instance.new("IntValue")
	aureus.Name = "aureus"
	aureus.Value = 0
	aureus.Parent = leaderstats
	local playerUserId = player.UserId -- Not .Name use .UserId as they can change name
	--Load Data
	local data 
	local success, errormessage = pcall(function()
		data = dataStore1:GetAsync(playerUserId)
	end)
	aureus.Value = data
	if success then
		if data then
			aureus.Value = data.aureus -- sets cash to data
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId 
	local data = 
		{
			aureus = player.leaderstats.aureus.Value,
		}
	local success,errormessage = pcall(function()
		dataStore1:SetAsync(playerUserId,data) 
	end)
	if success then
		print("data successfully saved")
	else
		print("Error when saving data")
		warn(errormessage)
	end
end)
remoteEvent.OnServerEvent:Connect(game.ReplicatedStorage.updateMoney)

This is my working datastore. It works to everything I want it to. How do I create a remote event that updates the server side information from the client clicking the button? Reading the API I understand a remoteevent can be used for this exact purpose. In replicated storage I made my event updateMoney. Obviously my arguments don’t work and that’s completely because I’m not sure really how to connect the remote event from local script to server script. Does anyone have any advice or tips on how I can properly connect my local and server script with the remote event? Reading the API for advice shows we pass an argument from the two scripts via the event. I tried inputting my arguments but it also didn’t work. Then I tried using the reference to the event itself but obviously that accomplishes nothing because I need to pass the argument or value I want to update,

remoteEvent:FireServer(game.ReplicatedStorage.updateMoney)
remoteEvent.OnServerEvent:Connect(game.ReplicatedStorage.updateMoney)

You’re passing your RemoteEvent to :Connect() and :FireServer(). You need to pass a function to :Connect() and arguments to :FireServer() instead, something like this:

-- locally
remoteEvent:FireServer(someArgument)

-- server
remoteEvent.OnServerEvent:Connect(function(player: Player, someArgument)
  -- do whatever you need to do
end)

Per the docs, whatever you pass to :FireServer() is passed as an argument to your OnServerEvent listener. Remember that the first argument of OnServerEvent will always be a Player instance.

As I stated above I understood that (game.ReplicatedStorage.updateMoney) was wrong. It was a placeholder because I’m not sure what arguments I need to pass. The question I had is what argument I pass to get my scripts to talk to one another. If I try player I get ‘leadedatats is not a valid member of players’.

Liked reading the docs and even watching this video How To Use Remote Events in Roblox - YouTube helps me to understand the concepts. But having my leaderstat inside a datastore I’m lost on how to connect my data. I can do it without it being in a datastore but I’m honestly lost on how to utilize remote events inside a datastore of my leaderstat.

To remove events that update a player’s leaderstats datastore in Roblox Studio, you first need to identify the events that are being used to update the datastore. These events could be listeners for button clicks, key presses, or any other user input that triggers the update.

Once you know which events are being used, you can remove them using the Disconnect method. Here’s an example of how to remove a listener that updates a player’s leaderstats datastore when a button is clicked:

-- Assume the button is named "MyButton" and the leaderstats object is named "leaderstats"

-- Define the function that updates the datastore
local function updateDataStore()
    local player = game.Players.LocalPlayer
    local leaderstats = player:WaitForChild("leaderstats")
    leaderstats.MyValue.Value = leaderstats.MyValue.Value + 1
end

-- Connect the function to the button's click event
local myButton = script.Parent.MyButton
myButton.MouseButton1Click:Connect(updateDataStore)

-- Later, when you want to remove the listener:
myButton.MouseButton1Click:Disconnect(updateDataStore)

In this example, we define a function called “updateDataStore” that retrieves the player’s leaderstats object and increments the value of a property called “MyValue”. We then connect this function to the “MouseButton1Click” event of the “MyButton” object using the “Connect” method.

To remove the listener later, we call the “Disconnect” method on the “MouseButton1Click” event of the “MyButton” object and pass in the “updateDataStore” function as an argument.

Note that if you have multiple listeners attached to the same event, you’ll need to call “Disconnect” on each one individually.

1 Like

Ok I understand most of what you said. I made

local function updateDataStore()
	local player = game.Players.LocalPlayer
	local leaderstats = player:WaitForChild("leaderstats")
	leaderstats.aureus.Value = leaderstats.aureus.Value + 1
end

And this should work to a degree. I made the function update. I grabbed my players. I got the leaderstats child and we use waitforchild as otherwise it would try to grab it before it loads. Updating the leaderstats value with the .aureus makes sense aureus will be a child of the leaderstats. I don’t understand the bottom. In my button script I have this.

This doesn’t add up for me. I’m lost. The top part of your code makes sense but I’m completely lost on the bottom. Where do I put my remoteEevent? I made the remoteEvent “updateMoney” but I don’t get where I’d connect them? I’m sorry if this sounds arrogant but I’m massively lost on how to connect. Like in my local script inside the button obviously updateDataStore gives me a red line because it doesn’t know what to reference.

button.MouseButton1Up:Connect(function()
	print("also clicked")
	local leaderstats = player.leaderstats
	local aureus = leaderstats and leaderstats:FindFirstChild("aureus")
	if aureus then
		button.MouseButton1Up:Connect(updateDataStore)
	end    
end)```
replicatedStorage.updateMoney.OnServerEvent:Connect(function()
	local function updateDataStore()
		local player = game.Players.LocalPlayer
		local leaderstats = player:WaitForChild("leaderstats")
		leaderstats.aureus.Value = leaderstats.aureus.Value + 1
		local mB = game.StarterGui.ScreenGui.ClickMe
		mB.MouseButton1Up:Connect(updateDataStore)
	end
end)

I tried this but no errors and nothing happens.

first try figure out what is happening between the client and the server

-- local script inside of the button

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.RemoteEvent

local button = script.Parent

button.MouseButton1Click:Connect(function()
	local argument1 = "arg1"
	local argument2 = "arg2"
	event:FireServer(argument1, argument2)
end)
-- server script inside of server script service

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(player, argument1, argument2)
	print(player.Name..", "..argument1..", "..argument2)
end)
1 Like

Ok so using your method I was able to get the prints. Trying to translate this to updating my leaderstats however I am confused.

button.MouseButton1Click:Connect(function()
	local leaderstats = player.LocalPlayer:WaitForChild("leaderstats")
	local aureus = leaderstats and leaderstats:FindFirstChild("aureus")
	if aureus then
		replicatedStorage.updateMoney:FireServer(aureus)
	end  
end)

Ok so my above script grabs my leaderstats after its made. Grabs the child of leaderstats which is aureus. The arguement is aureus just like you had arg1 and arg2. Going to my leaderstats script I have

remoteEvent.OnServerEvent:Connect(function(aureus,leaderstats)
	leaderstats.aureus.Value = leaderstats.aureus.Value + 1
end)

What am I missing? I get flooded the error of “aureus is not a valid member of IntValue “Players.RazeVenStudios.leaderstats.aureus””. I know I’m completely failing to understand this. I just want to understand it. Your example makes perfect sense. Trying to translate that into my datastore leaderstat I’m a complete failure.

Like for my first bit of code in the button this what I understand

button.MouseButton1Click:Connect(function()
	local leaderstats = player.LocalPlayer:WaitForChild("leaderstats")
	local aureus = leaderstats:FindFirstChild("aureus")
	if aureus then
		remoteEvent:FireServer(aureus)
	end  
end)

I am calling the button on mouse1click event. I’m grabbing my leaderstats from my player, and the aureus intValue. If the value equals “aureus” it connects to my event. Am I wrong or is that the correct understanding?

And inside my server script I have

remoteEvent.OnServerEvent:Connect(function(aureus)
	local player = game.Players.LocalPlayer
	local leaderstats = player.LocalPlayer:WaitForChild("leaderstats")
	leaderstats.aureus.Value = leaderstats.aureus.Value + 1
end)

What am I doing wrong? How are the events not connecting?
remoteEvent is firing on the server event. Argument being passed is aureus. I define the player and leaderstats again and update.

something more like this if you’re trying to increase a leaderstat value with a button

-- local script

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.RemoteEvent

local button = script.Parent
local amountToIncrease = 10

button.MouseButton1Click:Connect(function()
	event:FireServer(amountToIncrease)
end)
-- server script

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(player, amountToIncrease)
	player.leaderstats.aureus.Value += amountToIncrease
end)
1 Like

Thank you so much for your help. I really appreciate it. I finally got it to work after all of your advice.

Inside my local script is

button.MouseButton1Click:Connect(function()
	local leaderstats = player.LocalPlayer:WaitForChild("leaderstats")
	local aureus = leaderstats:FindFirstChild("aureus")
	if aureus then
		remoteEvent:FireServer(aureus,leaderstats)
	end  
end)

and inside my datastore script it is

remoteEvent.OnServerEvent:Connect(function(player,aureus,leaderstats)
	local leaderstats = player:WaitForChild("leaderstats")
	local aureus = leaderstats:FindFirstChild("aureus")
	leaderstats.aureus.Value = leaderstats.aureus.Value + 1
end)

And it works. I really appreciate the help Alola.

for that you need to reference the player in the function part and because its part of a server script, you dont need to mention “game.Players.LocalPlayer”. while you can also find the player and change there data from there

no problem but you arent changing the players stat

///////////////
///////////////

What do you mean? It updates my leaderstat and my datastore successfully saves the data? What did I do wrong? Even when I kick myself with game.Players.(ign):Kick() it succesfully saves my leaderstat value and looking at my leaderstat intValue it also saves.

image
I pressed the button a few times, kicked myself, and lauched again and it saved.
image

Whats wrong?

should’ve been

player.leaderstats.aureus.Value += 1

They both work. Is the player.leaderstats.aureus.Value += 1 the better way to do it? Because as of right now in my script both lines work.

well the player is the reference from clicking the button