Thank you! It works very well!
Now here’s what I’ve done on my side:
SarterGUI, LocalScript (The button to hide players):
local Button = script.Parent.ScreenGui.TextButton
local Event = game:WaitForChild("ReplicatedStorage"):WaitForChild("HidePlayersEvent")
Button.MouseButton1Click:Connect(function()
if Button.BackgroundColor3 == Color3.fromRGB(255, 255, 255) then
Event:FireServer("Hide")
Button.BackgroundColor3 = Color3.fromRGB(63, 63, 63)
elseif Button.BackgroundColor3 == Color3.fromRGB(63, 63, 63) then
Event:FireServer("Show")
Button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
else
warn("Error :( (Client)")
end
end)
ServerScriptService, Script (Gets the Event from the Client and fires it back to activate the setting locally):
game:WaitForChild("ReplicatedStorage"):WaitForChild("HidePlayersEvent").OnServerEvent:Connect(function(player, Status)
if Status == "Hide" and player then
game:WaitForChild("ReplicatedStorage"):WaitForChild("HidePlayersEvent"):FireClient(player, "HideFromServer")
print("Hidden all players, Server")
elseif Status == "Show" and player then
game:WaitForChild("ReplicatedStorage"):WaitForChild("HidePlayersEvent"):FireClient(player, "ShowFromServer")
print("Showen all players, Server")
else
warn("Error :( (Server)")
end
end)
StarterPlayerScripts, LocalScript (Hides players locally after getting the Event from the Server):
--!strict
local Players = game:GetService("Players")
local invisibilityConnections = {}
local function setCharacterInvisible(character: Model, invisible: boolean)
local transparency = if invisible then 1 else 0
for _, descendant in character:GetDescendants() do
if not (descendant:IsA("BasePart") or descendant.ClassName == "Decal") then
continue
end
if descendant.Name == "HumanoidRootPart" then
continue
end
(descendant :: BasePart).Transparency = transparency
end
end
local function onCharacterAdded(character: Model)
setCharacterInvisible(character, true)
end
local function onPlayerAdded(player: Player)
local character = player.Character
if character then
onCharacterAdded(character)
end
invisibilityConnections[player] = player.CharacterAdded:Connect(onCharacterAdded)
end
local function onPlayerRemoving(player: Player)
invisibilityConnections[player]:Disconnect()
invisibilityConnections[player] = nil
end
local function setOthersInvisible(invisible: boolean)
for _, connection in invisibilityConnections do
connection:Disconnect()
end
invisibilityConnections = {}
for _, player in Players:GetPlayers() do
if player == Players.LocalPlayer then
continue
end
if invisible then
invisibilityConnections[player] = player.CharacterAdded:Connect(onCharacterAdded)
invisibilityConnections.PlayerAdded = Players.PlayerAdded:Connect(onPlayerAdded)
invisibilityConnections.PlayerRemoving = Players.PlayerRemoving:Connect(onPlayerRemoving)
end
if player.Character then
setCharacterInvisible(player.Character, invisible)
end
end
end
---------------------------------------------------------
game:WaitForChild("ReplicatedStorage"):WaitForChild("HidePlayersEvent").OnClientEvent:Connect(function(Status)
if Status == "HideFromServer" then
setOthersInvisible(true)
print("Client side hiding complete!")
elseif Status == "ShowFromServer" then
setOthersInvisible(false)
print("Client side showing complete!")
else
warn("Error :( (Client, PlayerScripts)")
end
end)
Also, the same principle should apply to parts in game.Workspace
, right?