让一个玩家只对某些玩家不可见的关键是要理解LocalScript 中对世界所做的任何更改只有该玩家可见。它们不会在其他客户端之间复制。因此,如果我们可以设置一个系统,可以告诉 LocalScripts 使特定玩家不可见,那么该玩家将从他们的屏幕上消失,并且只会从他们的屏幕上消失。
所以如果我们有这个系统,玩家二可以说,“嘿,游戏服务器,你能不能让人们知道让我隐形!”。该消息将发送到服务器,服务器会将其发送到一堆不同的客户端,每个客户端将使用它们的 LocalScripts 在本地进行更改,并且玩家实际上会在他们的屏幕上变得不可见。
因此,即使玩家 2 看起来对玩家 1 和 3 来说是不可见的,但对于游戏服务器来说,并没有什么不同或不寻常的地方,这些变化完全是针对每个玩家的世界版本进行本地化的。
一种方法是使用这样的设置:
在 LocalScript 中,你会有这样的东西:
local tool = script.Parent
local IsHidden = tool:WaitForChild("IsHidden", 5)
local TogglePlayerVisible = game.ReplicatedStorage.TogglePlayerVisible
local localPlayer = game.Players.LocalPlayer
-- debug
tool.Name = "Turn Invisible"
tool.RequiresHandle = false
IsHidden.Value = false
-- 1) When a player activates the tool, flip the BoolValue
tool.Activated:Connect(function()
IsHidden.Value = not IsHidden.Value
tool.Name = IsHidden.Value and "Become Visible" or "Turn Invisible"
end)
-- 2) When the BoolValue changes, tell the server about it
IsHidden.Changed:Connect(function(updatedValue)
TogglePlayerVisible:FireServer(updatedValue)
end)
-- 4) When the server tells us a player has used the tool, make that player invisible locally
TogglePlayerVisible.OnClientEvent:Connect(function(player, isHidden)
local invisible = 1.0
local visible = 0.0
-- if we are the one who sent it, only make us a little invisible
if (player.Name == localPlayer.Name) then
invisible = 0.7
end
-- loop over all of the parts in a player's Character Model and hide them
for _, part in ipairs(player.Character:GetDescendants()) do
local shouldTogglePart = true
-- handle exceptions
if not (part:IsA("BasePart") or part:IsA("Decal")) then
shouldTogglePart = false
elseif part.Name == "HumanoidRootPart" then
shouldTogglePart = false
end
-- hide or show all the parts and decals
if shouldTogglePart then
part.Transparency = isHidden and invisible or visible
end
end
end)
在脚本中,你会有这样的东西:
local TogglePlayerVisible = game.ReplicatedStorage.TogglePlayerVisible
-- 3) whenever a player triggers this event, send it out to all players
TogglePlayerVisible.OnServerEvent:Connect(function(player, isVisible)
TogglePlayerVisible:FireAllClients(player, isVisible)
end)
如果您只希望特定个玩家看不到某个玩家,那么您可以修改第 3 步,这样您就可以专门选择将消息发送给哪些玩家,而不是使用TogglePlayerVisible:FireAllClients FireClient.