通过单击按钮让玩家组队(roblox studio)

问题描述 投票:0回答:1

我正在尝试这个代码

local player = game.Players.LocalPlayer
script.Parent.MouseClick:Connect(function()
    -- Change team color to "Really Blue"
    player.TeamColor = BrickColor.new("Really blue")

    -- Kill the player
    local character = player.Character
    if character then
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.Health = 0
        end
    end
end)

但由于某种原因,当我按下按钮时,我没有被组队。 我是编码新手,语言是 lua (roblox studio)

我尝试过几次将颜色更改为不同的东西,但结果都是相同的。

lua roblox
1个回答
0
投票

您无法在客户端(在 LocalScript 中)更改玩家的团队,因此您需要使用 RemoteEvents 来更改玩家的团队。

首先,将 RemoteEvent 添加到 ReplicatedStorage。

然后,LocalScript 应该看起来像这样:

local rs = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
script.Parent.MouseClick:Connect(function()
    rs.Event:FireServer()
end)

现在,对于服务器脚本:

local rs = game:GetSerivce("ReplicatedStorage")
rs.Event.OnServerEvent:Connect(function(plr)
  player.TeamColor = BrickColor.new("Really blue")
  local character = plr.Character
  if character then
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.