Client Script

LocalScript (Client Script)

A LocalScript is a piece of code that runs only on the player's computer (client).

It deals with things specific to the local player, like user interface interactions, camera controls, and effects that don't need to be seen by everyone.

User Interface (UI) InteractionUpdating or interacting with GUI elements
Player InputPlayer inputs such as keyboard, mouse, or gamepad events.
Local EffectsVisual or audio effects that are specific to the local player
Local Data HandlingLocal settings or preferences, inventory management
ResponsivenessRuns on the client, which makes it much more responsive to the player's actions. The delay is minimal as it does not require communication with the server for detecting the input.
EfficiencyHandles the event locally, reducing the load on the server, which is particularly important in games with many players.
-- LocalScript in StarterPlayerScripts
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer

local givePointsEvent = ReplicatedStorage:WaitForChild("GivePointsEvent")

local function triggerGivePoints(points)
    givePointsEvent:FireServer(points)
end

-- Example usage: Trigger the event when the player clicks a GUI button
local screenGui = Instance.new("ScreenGui", player:WaitForChild("PlayerGui"))
local button = Instance.new("TextButton", screenGui)
button.Size = UDim2.new(0, 200, 0, 50)
button.Position = UDim2.new(0.5, -100, 0.5, -25)
button.Text = "Get Points"

button.MouseButton1Click:Connect(function()
    triggerGivePoints(10)
end)
-- Script in ServerScriptService

-- Get the RemoteEvent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local givePointsEvent = ReplicatedStorage:WaitForChild("GivePointsEvent")

-- Function to handle the remote event
local function onGivePoints(player, points)
    -- Add points to the player's leaderstats
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local score = leaderstats:FindFirstChild("Points")
        if score then
            score.Value = score.Value + points
            print(player.Name .. " received " .. points .. " points!")
        end
    end
end

-- Connect the function to the OnServerEvent of the remote event
givePointsEvent.OnServerEvent:Connect(onGivePoints)

If you found this tutorial helpful and would like to support my work, please consider buying me a coffee.

Thank you very much for your support!

Buy me a coffee