Hello! I’m new to the DevForum so please let me know if I need to change where the category of this post!
So, I’m making a title screen that shows the player sitting on a ledge looping an animation, but one thing that I noted right from the start is that multiple players would be stacked onto each other, and I need to figure out a way to hide all the players for the local player while they’re in the title. I’ve looked around the forum for a while, but never found a concrete solution that didn’t involve updating the server every tick. I’m new, so let me know if my worries are misplaced, but I think doing that would hinder performance a TON. Can someone provide me a solution to this? P.S. : Visual examples are appreciated!
Hey! perhaps something like this. When first joining, it is in loading screen so it hides all players and after 5 seconds it unhides them.
local InLoadingScreen = true
local function hidePlayer(character : Model, show)
for i,part in ipairs(character:GetDescendants()) do
if part ~= character.PrimaryPart then
if part:IsA("BasePart") or part:IsA("Decal") then
if not show then
part.Transparency = 1
else
part.Transparency = 0
end
end
end
end
end
--when joining the game
for i,v in ipairs(game.Players:GetPlayers()) do
pcall(function()
hidePlayer(v.Character)
end)
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
if InLoadingScreen then
hidePlayer(char)
end
end)
end)
task.wait(5)
--after a while
InLoadingScreen = false
for i,v in ipairs(game.Players:GetPlayers()) do
pcall(function()
hidePlayer(v.Character, true)
end)
end
Didn’t seem to work, both players are still visible to each other. I would provide any errors it gave, but it didn’t give any. Just to verify, I had this in a LocalScript under StarterPlayerScripts, and just copied the above code. Was I supposed to do something differently?
Here’s a simple solution! Run this code on the server. It will reparent all existing and newly added characters to Workspace.Characters. Add this folder in edit mode, or else the code won’t work!
local Players = game:GetService("Players")
local Characters = workspace.Characters
--\\ Private Methods
local function onCharacter(character: Model?)
if not character then return end
character.Parent = Characters
end
local function onUser(user: Player)
task.spawn(onCharacter, user.Character)
user.CharacterAdded:Connect(onCharacter)
end
--\\ Connections
do -- Reparent Characters
Players.PlayerAdded:Connect(onUser)
for _, user in Players:GetPlayers() do
task.spawn(onUser, user)
end
end
Now to make use of this, all you need to do is reparent the Characters folder on the client to hide everyone!
local Characters = workspace:WaitForChild("Characters")
-- to hide everyone:
Characters.Parent = nil
-- to bring everyone back:
Characters.Parent = workspace
Obviously hiding your own character will make it impractical to play, but since you’re using this for a lobby screen or cutscene, that shouldn’t be a problem. If you need to keep the local player’s character visible, you could reparent each character manually, ignoring the local player’s character. To do this, use this code on the client.
local Players = game:GetService("Players")
local Characters = workspace:WaitForChild("Characters")
local areOthersHidden = false
local player = Players.LocalPlayer
--\\ Private Methods
local function setOthersHidden(enabled: boolean)
if areOthersHidden == enabled then return end
areOthersHidden = enabled
local parent = if areOthersHidden
then nil
else Characters
for _, user in Players:GetPlayers() do
if user == player then continue end
user.Character.Parent = parent
end
end
local function onCharacter(character: Model?)
if not character then return end
character.Parent = if areOthersHidden
then nil
else Characters
end
local function onUser(user: Player)
task.spawn(onCharacter, user.Character)
user.CharacterAdded:Connect(onCharacter)
end
--\\ Connections
do -- Manage Character Visibility
Players.PlayerAdded:Connect(onUser)
for _, user in Players:GetPlayers() do
task.spawn(onUser, user)
end
end
I got a bit of an odd result. In player 1’s view, player 2 was completely visible, with player 1’s body parts being invisible, but their accessories were visible. Same-ish thing on player 2’s view, where they were partially visible, but player one only had 1 accessory visible. It was quite odd.
Any idea of what might be wrong? The LocalScript is located in StarterCharacterScripts. (I opted for the version that supposedly didn’t remove the LocalPlayer, at least from what I read.)
Edit: Trying something, I might’ve made a stupid mistake…
I recommend putting the code I provided in StarterPlayerScripts or ReplicatedFirst, just not in StarterCharacterScripts. Either way though, it works fine for me. Could you show me how you integrated the code?
Never mind, it worked! I had changed the boolean at the start to true, since I was too lazy to set up anything. I changed it, and it worked. Thank you so much! Question, do you reckon this code would work in a module script for organization’s sake?