【发布时间】:2026-01-02 04:20:05
【问题描述】:
我正在按照这个问题How would I make a player invisible only to certain players? 中的示例进行操作,但我无法使其适应我的需要。
我将如何使某个团队/某个组中的某个玩家只看到一半的玩家?以及如何让其他隐形玩家看到隐形玩家?
【问题讨论】:
我正在按照这个问题How would I make a player invisible only to certain players? 中的示例进行操作,但我无法使其适应我的需要。
我将如何使某个团队/某个组中的某个玩家只看到一半的玩家?以及如何让其他隐形玩家看到隐形玩家?
【问题讨论】:
在上一个代码示例的代码的基础上,您只需修改服务器告诉所有客户端显示或隐藏播放器的最后一步。
因为你试图满足两个条件:
即使不可见,也可以看到队友,并且
当你也隐形时可以看到隐形玩家
您需要添加一些条件来跟踪人员所在的团队以及谁是主动隐身的。当您的一位队友隐身时,我们只会部分隐藏他们。而当我们本地玩家变为隐形时,我们需要遍历所有隐藏的玩家并显示它们。
该代码可能如下所示:
-- make a helper function to reveal / hide a player
local function setVisiblity(player, isHidden, showPartial)
local invisible = 1.0
local visible = 0.0
-- if showing partially, make invisibility slightly more visible
if (showPartial == true) 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
-- make a table to hold onto the hidden players
local invisiblePlayers = {} -- <playerName, playerObject>
TogglePlayerVisible.OnClientEvent:Connect(function(player, isHidden)
-- flip the visibility of the player that requested the change
local showPartial = false
-- keep track of who is visible / invisible
if (isHidden) then
invisiblePlayers[player.Name] = player
else
invisiblePlayers[player.Name] = nil
end
-- reveal the invisible players if it is you
if (player.Name == localPlayer.Name)
showPartial = true
-- if we are now invisible, reveal all the other invisible players, otherwise hide them
local canSeeOtherInvisibleUnits = false
if (isHidden) then
canSeeOtherInvisibleUnits = true
end
-- loop over the invisible players and reveal or hide them
for name, invisPlayer in pairs(invisiblePlayers) do
setVisible(player, canSeeOtherInvisibleUnits, true)
end
-- reveal the other player if we're on the same team
elseif (player.TeamColor == localPlayer.TeamColor)
showPartial = true
-- reveal the other player if we are currently invisible
elseif (invisiblePlayers[localPlayer.Name] ~= nil)
showPartial = true
end
-- toggle that player's invisibility
setVisiblity(player, isHidden, showPartial)
end)
【讨论】: