クライアントスクリプト

LocalScript (クライアントスクリプト)

LocalScript は、プレイヤーのコンピュータ(クライアント)でのみ実行されるコードです。

ローカルプレイヤーに特有の処理を行います。例えば、ユーザーインターフェースの操作、カメラ制御、全員が見る必要のないエフェクトなどです。

ユーザーインターフェース (UI) の操作GUI 要素の更新や操作
プレイヤー入力キーボード、マウス、またはゲームパッドのイベントなどのプレイヤー入力。
ローカルエフェクトローカルプレイヤーに特有の視覚効果や音声効果
ローカルデータ処理ローカル設定や好み、インベントリ管理
応答性クライアント上で実行されるため、プレイヤーの操作に非常に迅速に反応します。入力検出のためにサーバーとの通信を必要としないため、遅延が最小限に抑えられます。
効率性イベントをローカルで処理することで、サーバーの負荷を軽減します。特にプレイヤーが多いゲームでは重要です。
-- 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)

このチュートリアルがお役に立ちましたら、私の仕事をサポートするためにコーヒーをおごっていただけると嬉しいです。

ご支援、誠にありがとうございます!

コーヒーをおごる