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)