Local Scripts and Server Scripts

I get LocalScripts and ServerScripts, but how do you connect functions between server and client with those types of scripts?

What is the method used to have server editing clients, and clients editing server related things such as typing something into a GUI, etc?

1 Like

This can be achieved with a RemoteEvent.

I’m in my phone so I won’t be much help, but you can find out more in here.

1 Like

Ahh okay because like I am trying to achieve a GUI shop script that gives a player a tool, on top of that I’d have to get the money system working so knowing this would be helpful

Hello, There are tons of tutorials but I will try to explain as simple as I can. So, you’ll typically use RemoteEvents and RemoteFunctions. These allow you to send messages and data between the server and client securely.

Here’s a basic breakdown:

  1. RemoteEvents
  • Used for one-way communication.
  • Example: Telling the server that a client clicked a button.
  1. RemoteFunctions
  • Used for two-way communication.
  • Example: Asking the server how much money a player has and waiting for a response.

Let’s say you have a GUI on the client where players can buy a tool. When a player clicks the “Buy” button, you want to:

  1. Get the player’s money.
  2. Give the player the tool if they have enough money.

Server: (Note this is just an example, you should change some stuff in order to work)

  1. Create a RemoteEvent in ReplicatedStorage named “BuyToolEvent”.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local BuyToolEvent = ReplicatedStorage:WaitForChild("BuyToolEvent")

BuyToolEvent.OnServerEvent:Connect(function(player, toolName)
    local playerMoney = player:WaitForChild("leaderstats"):WaitForChild("Money").Value
    local toolCost = 100 -- This can be dynamic based on the tool

    if playerMoney >= toolCost then
        player:WaitForChild("leaderstats"):WaitForChild("Money").Value = playerMoney - toolCost
        local tool = game.ServerStorage:FindFirstChild(toolName):Clone()
        tool.Parent = player.Backpack
    else
        -- Player doesn't have enough money
    end
end)

Client:

When the player clicks the “Buy” button:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local BuyToolEvent = ReplicatedStorage:WaitForChild("BuyToolEvent")

local buyButton = script.Parent -- Assuming this script is a child of the button

buyButton.MouseButton1Click:Connect(function()
    BuyToolEvent:FireServer("ToolName") -- Replace "ToolName" with the name of the tool the player is trying to buy
end)

If you need a response from the server (e.g., to know if the purchase was successful), you’d use a RemoteFunction instead of a RemoteEvent. The client would call the function, the server would process the request, and then the server would return a response to the client. I think this will help you! :slight_smile:

1 Like

This is very helpful, thank you

1 Like

Not going to be offensive, but I think that was ChatGPT.

Good explanation nevertheless…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.