A tutorial for server and client communication

The player is called a client, while everybody together is called the server.
The server is used for damaging players, NPC behavior, LoadCharacterAsync, and much more.
The client is used for GUI/UI, handing local movement, and if you’re a good developer, visual effects.

Please be aware that I’m a beginner scripter and this information may be slightly wrong.

Okay. First, the player clicks a GUI button. We want it to do something on everybody’s screen.
How are we going to do that?

Well we need a LocalScript inside of the ImageButton or TextButton.

script.Parent.MouseButton1Up:Connect(function()
	game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("RemoteEvent"):FireServer()
end)

Assuming you have a folder in ReplicatedStorage called Remotes, which I recommend to do since it organises everything. RemoteEvent is the RemoteEvent instance, but you should name it something else to make it cleaner, such as BuyItem.

script.Parent.MouseButton1Up:Connect(function()
	game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("BuyItem"):FireServer()
end)

Now how does the server give that player the item?
I’ll paste the script below, but please be aware you can’t just put it in your game straight away.
Without organizing everything and having basic scripting knowledge, this won’t do anything.

local item = game.ReplicatedStorage:WaitForChild("Items"):WaitForChild("ToolNameHere")

local v = 1

game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("BuyItem").OnServerEvent:Connect(function(player)
	if player and player:IsA("Player") and player:WaitForChild("leaderstats").Money.Value >= item:GetAttribute("Price") then
		print(player.Name.. "brought ".. item.Name)
		if v == 1 then
			item:Clone().Parent = player:FindFirstChildOfClass("Backpack") or player.Character
		else
			item:Clone().Parent = player.Character
		end
	else
		print(player.Name.. "didn't have enough money to buy".. item.Name)
	end
end)

Alright, now we know how to make the client tell the server information, but what about vice-versa?

FireServer() won’t work, it must be FireClient()?
Well yeah, but its more advanced than that.
How would FireClient() know which player? will it tell every client?
No, that would be FireAllClients()
FireClient(game.Players.PlayerName) would be correct.

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("ServerToClient"):FireClient(game.Players:FindFirstChildOfClass("Player"))

or for everybody,

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("ServerToClient"):FireAllClients()

Now how will the client know when the server tells it something?
Like this:

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("ServerToClient").OnClientEvent:Connect(function()
	--omg the server told us something
	print("omg omg omg")
end)
2 Likes

Hi, just wanted to help polish this off a bit. Firstly, i would use script.Parent.Activated instead of script.Parent.MouseButton1Up, as that makes it that if s mobile player clicks it, it still works, same with PC. Also, with the

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("ServerToClient"):FireClient(game.Players:FindFirstChildOfClass("Player"))

this will send it to the first player in the players, which will work fine when it’s one player in studio or in game, but if there’s a lot, you may want to find a different way of determining which player to send to.

MouseButton1Up works with mobile too and is used for GUI buttons, Activated is used for tools

:locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked::locked:
Haha lock.
Lock
lock.
Lock
:locked::locked::locked::locked::locked:
Lock.
Lock lock lock!!
Locklock lock clock lock llo lock clockl lokkk clol locl lock lock lock lcko

what?? :sob:

“Yes, MouseButton1Up generally works on mobile for Roblox, as it maps to the end of a screen tap (releasing the finger). While not designed specifically for touch, Roblox converts input events, allowing it to function for GUI elements, though it may not always be reliable in the mobile studio environment.
Key Considerations for Mobile Input:
Behavior: MouseButton1Up fires when a user lifts their finger off a UI button.
Alternative Events: For better, more consistent mobile support, use .Activated or InputEnded via UserInputService.” This is what i got online, I cba to type all of it, as ikk that . Activated is more reliable, but ur right mouse button 1up works

im sorry blame the lock :locked::locked: xd uhhh theres lore trust

alr sounds weird but ok

no its the locks fault yk :locked:trust me itll stalk you in the roblox chat js wait

what is ts :sob:

1 Like

cmon, join a random roblox game and wait, there will be a lock :locked:

oh i js realised what it is in the announcements for ppl chatting in different age groups or smh

no its the lock :locked:

admit it, say “fine”, nothing else.

yeah, have you also seen the devforum verification one as well? I’ve already verified but i kinda wish i didn’t. Have u?

uh ikk what ur talking abt, ia anyone else seeing ts?

yes

anyone other than u im but alr :victory_hand:

what is the purpose of these replies

1 Like

thats what im wondering bro