Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i learn how to use Remote events?

i am new to scripting and want to make a saving leaderboard. i have already scripted an auto saving leaderboard that looks something like this.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") 

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Currency = Instance.new("IntValue", Leaderstats)
	Currency.Name = "Money" 
	Currency.Value = 0	
	
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Currency.Value = Data 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Money.Value) 
end)

and i want to make a UI button that adds 10 money to the leaderboard when clicked but i can't do that using local scripts so i have to use a remote to do it. but here comes the problem. I don't know how to trigger them and what terms and parameters to use

2 comments

0

tanks the second one was the one i was already following

1 answer

✓ Accepted answer

Ok so basically we are using the RemoteEvent so that the server can add a point for the player, thus everybody else seeing it.

To start off you would want a TextButton with a LocalScript with:

local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("Cash")

script.Parent.MouseButton1Click:connect(function() 
	Event:FireServer() 
end)

And then you want a script listening to the event with:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") --Get your event

Event.OnServerEvent:connect(function(player) --First paramter is always the player that fired it
	local leaderstats = player.leaderstats
	local cash = leaderstats.Money
	
	cash.Value = cash.Value + 10
end)

Explanation

So basically, when we fire a RemoteEvent the first parameter is always the player that fired it, so what if you wanted to pass other parameters through? Well here is an example:

local RP = game:GetService("ReplicatedStorage")
local Event = RP:WaitForChild("Cash")

script.Parent.MouseButton1Click:connect(function() 
	Event:FireServer("Hello I am existing")  --Lets say we wanted to to pass another parameter to the server, for this example, a string.
end)

And for the script what we would do is:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") --Get your event

Event.OnServerEvent:connect(function(player, var2) --First paramter is always the player that fired it
	local leaderstats = player.leaderstats
	local cash = leaderstats.Money
	
	cash.Value = cash.Value + 10
	print(var2)
end)

Output: Hello I am existing.

This isn't the best explanation due to the time span that I had to write this but here is the article about RemoteEvents or you can read another explanation by killerbrenden

2 comments

1

I wrote a basic answer on a question that simply explains RemoteEvents, since the Wiki can be hard to understand at some points. https://scriptinghelpers.org/questions/94651/can-someone-help-me-understand-remote-events-they-dont-work-for-me#87427

0

the reason i asked this is because i thought i knew what parameters are but i didn't i figured it out in the end with the help of a pro scripter but this was also a verry good explanation. thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.