LocalScript (客户端脚本)
Empty
Empty
Empty
LocalScript 是一段仅在玩家的计算机(客户端)上运行的代码。
Empty
Empty
Empty
它处理与本地玩家相关的事情,比如用户界面交互、摄像机控制和不需要所有人看到的效果。
Empty
Empty
Empty
用户界面 (UI) 交互 | 更新或与 GUI 元素交互 |
玩家输入 | 玩家输入,例如键盘、鼠标或游戏手柄事件。 |
本地效果 | 特定于本地玩家的视觉或音频效果 |
本地数据处理 | 本地设置或偏好,库存管理 |
Empty
Empty
Empty
响应速度 | 在客户端上运行,使其对玩家的操作响应更加灵敏。延迟最小,因为它不需要与服务器通信以检测输入。 |
效率 | 在本地处理事件,减轻服务器负担,这在有许多玩家的游戏中尤为重要。 |
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)