LocalScript (Client Script)
Empty
Empty
Empty
A LocalScript is a piece of code that runs only on the player's computer (client).
Empty
Empty
Empty
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.
Empty
Empty
Empty
User Interface (UI) Interaction | Updating or interacting with GUI elements |
Player Input | Player inputs such as keyboard, mouse, or gamepad events. |
Local Effects | Visual or audio effects that are specific to the local player |
Local Data Handling | Local settings or preferences, inventory management |
Empty
Empty
Empty
Responsiveness | Runs 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. |
Efficiency | Handles the event locally, reducing the load on the server, which is particularly important in games with many players. |
Empty
Empty
Empty
-- 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)
Empty
Empty
Empty
-- 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