โ† Back to all scripts

RemoteEvent: Client to Server

Safe template for client-to-server communication with server-side validation.

NetworkingIntermediateServerScriptService + LocalScript
remoteevent-client-to-server.lua
-- RemoteEvent: Client to Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local myEvent = Instance.new("RemoteEvent")
myEvent.Name = "MyEvent"
myEvent.Parent = ReplicatedStorage

myEvent.OnServerEvent:Connect(function(player, action, value)
    if typeof(action) ~= "string" then return end
    if typeof(value)  ~= "number"  then return end
    if action == "BuyItem" then
        local cash = player.leaderstats and player.leaderstats.Cash
        if cash and cash.Value >= value then
            cash.Value -= value
        end
    end
end)

-- CLIENT LocalScript:
-- local myEvent = ReplicatedStorage:WaitForChild("MyEvent")
-- myEvent:FireServer("BuyItem", 50)
Related scripts in Networking
RemoteFunction: Get Server DataIntermediate โ†’
Something broken or not working?
๐Ÿ”ง Try the AI Script Fixer โ†’