Grouping System: Only show certain players on player's list

For context: I am creating an animal survival game, which will have certain animals be able to form groups e.g dholes. Every animal character has its animal name stored into it:

Screenshot (4059)

  1. **What do you want to achieve?

Now, I only want animals with the value tiger for example, only to be able to see other players on the list that also have the the tiger value stored inside their character. In other words, I don’t wanna have bears be able to invite tigers to their group and vice versa. To prevent that I only want to show players on the list that are tiger players as well.

  1. What is the issue?

I don’t really know how to restrict the view scripting wise. So that only players with the same value can invite each other.

  1. What solutions have you tried so far?

Searched through dev forum.

Imo it would only make sense to change something in this script, as it’s responsible for cloning the player template including the invite button.

function update() -- we create a function which will keep the playerlist updated
	-- Clearing the frame
	for i, v in pairs(script.Parent:GetChildren())do -- we loop through the playerlist frame
		if v:IsA("Frame") then -- if the thing we're currently at is a frame then
			v:Destroy() -- we destroy it
		end
	end

	
	for i, player in pairs(game.Players:GetPlayers()) do -- we loop through the existing players
		local frame = game:GetService("ReplicatedStorage"):WaitForChild("InvitePlayer"):Clone() -- we clone the frame which represents a player and allows us to invite him/her to our crew
		frame.Parent = script.Parent -- we put it inside the playerlist frame
		frame.Name = player.Name -- we give it the same name as the player we're currently at
		frame.PlayerName.Text = player.Name -- we give the textlabel inside the frame the same name as the player we're currently at
		frame.Position = UDim2.new(0, 0, 0.02 * i, 0) -- we set its position
		frame.Invite.LocalScript.Disabled = false -- we enable the localscript inside the button 
	end
end

update() -- we call the update function

game.Players.PlayerAdded:Connect(function() -- when a new player has joined
	update() -- we call the update function
end)

game.Players.PlayerRemoving:Connect(function() -- when a player has left
	wait(2) -- we add this wait, to prevent glitches
	update() -- we call the update function
end)

Any help is appreciated!!!

Anyone got an idea how to fix this issue?

What type of value is that?
image

This one’s a string value, without having an actual value stored inside of it
Screenshot (4061)

Just check for a string value and compare names then.

Code:

local LocalPlayer = game:GetService("Players").LocalPlayer
local InvitePlayer = game:GetService("ReplicatedStorage"):WaitForChild("InvitePlayer")

local function update() -- we create a function which will keep the playerlist updated
	-- Clearing the frame
	for i, v in script.Parent:GetChildren() do -- we loop through the playerlist frame
		if v:IsA("Frame") then -- if the thing we're currently at is a frame then
			v:Destroy() -- we destroy it
		end
	end
	
	local animal = LocalPlayer.Character:FindFirstChildWhichIsA("StringValue").Name

	for i, player in game.Players:GetPlayers() do -- we loop through the existing players
		local otherAnimal = player.Character:FindFirstChildWhichIsA("StringValue").Name
		
		if animal ~= otherAnimal then
			continue
		end
		
		local frame = InvitePlayer:Clone() -- we clone the frame which represents a player and allows us to invite him/her to our crew
		frame.Name = player.Name -- we give it the same name as the player we're currently at
		frame.PlayerName.Text = player.Name -- we give the textlabel inside the frame the same name as the player we're currently at
		frame.Position = UDim2.new(0, 0, 0.02 * i, 0) -- we set its position
		frame.Invite.LocalScript.Disabled = false -- we enable the localscript inside the button 
		frame.Parent = script.Parent -- we put it inside the playerlist frame
	end
end

update() -- we call the update function

game.Players.PlayerAdded:Connect(update) -- when a new player has joined

game.Players.PlayerRemoving:Connect(function() -- when a player has left
	task.wait(2) -- we add this wait, to prevent glitches
	update() -- we call the update function
end)
1 Like

Oh thank you so much for your help!! It can be so easy but if you sit infront of your pc for a long time, istg I be trippin. Just one little thing occurs now:

I don’t know why it says nil, I double checked the whole script :thinking:

That comes from if the player doesn’t have a string value. I’ll add checks for that.

Code:

local LocalPlayer = game:GetService("Players").LocalPlayer
local InvitePlayer = game:GetService("ReplicatedStorage"):WaitForChild("InvitePlayer")

local function update() -- we create a function which will keep the playerlist updated
	local animal = LocalPlayer.Character:FindFirstChildWhichIsA("StringValue")
	
	if not animal then
		return
	end
	
	-- Clearing the frame
	for i, v in script.Parent:GetChildren() do -- we loop through the playerlist frame
		if v:IsA("Frame") then -- if the thing we're currently at is a frame then
			v:Destroy() -- we destroy it
		end
	end

	for i, player in game.Players:GetPlayers() do -- we loop through the existing players
		local otherAnimal = player.Character:FindFirstChildWhichIsA("StringValue")

		if not otherAnimal or animal.Name ~= otherAnimal.Name then
			continue
		end

		local frame = InvitePlayer:Clone() -- we clone the frame which represents a player and allows us to invite him/her to our crew
		frame.Name = player.Name -- we give it the same name as the player we're currently at
		frame.PlayerName.Text = player.Name -- we give the textlabel inside the frame the same name as the player we're currently at
		frame.Position = UDim2.new(0, 0, 0.02 * i, 0) -- we set its position
		frame.Invite.LocalScript.Disabled = false -- we enable the localscript inside the button 
		frame.Parent = script.Parent -- we put it inside the playerlist frame
	end
end

update() -- we call the update function

game.Players.PlayerAdded:Connect(update) -- when a new player has joined

game.Players.PlayerRemoving:Connect(function() -- when a player has left
	task.wait(2) -- we add this wait, to prevent glitches
	update() -- we call the update function
end)

Thank you so much again, it’s helping me a lot!! I just think that something’s off now, it’s saying the following:

I never had that before

It means that there is no character.

Code:

local LocalPlayer = game:GetService("Players").LocalPlayer
local InvitePlayer = game:GetService("ReplicatedStorage"):WaitForChild("InvitePlayer")

local function update() -- we create a function which will keep the playerlist updated
	local localCharacter = LocalPlayer.Character
	local animal = localCharacter and localCharacter:FindFirstChildWhichIsA("StringValue")

	if not animal then
		return
	end

	-- Clearing the frame
	for i, v in script.Parent:GetChildren() do -- we loop through the playerlist frame
		if v:IsA("Frame") then -- if the thing we're currently at is a frame then
			v:Destroy() -- we destroy it
		end
	end

	for i, player in game.Players:GetPlayers() do -- we loop through the existing players
		local character = player.Character
		local otherAnimal = character and character:FindFirstChildWhichIsA("StringValue")

		if not otherAnimal or animal.Name ~= otherAnimal.Name then
			continue
		end

		local frame = InvitePlayer:Clone() -- we clone the frame which represents a player and allows us to invite him/her to our crew
		frame.Name = player.Name -- we give it the same name as the player we're currently at
		frame.PlayerName.Text = player.Name -- we give the textlabel inside the frame the same name as the player we're currently at
		frame.Position = UDim2.new(0, 0, 0.02 * i, 0) -- we set its position
		frame.Invite.LocalScript.Disabled = false -- we enable the localscript inside the button 
		frame.Parent = script.Parent -- we put it inside the playerlist frame
	end
end

update() -- we call the update function

game.Players.PlayerAdded:Connect(update) -- when a new player has joined

game.Players.PlayerRemoving:Connect(function() -- when a player has left
	task.wait(2) -- we add this wait, to prevent glitches
	update() -- we call the update function
end)

Oh so for better understanding, you’re saying that when something like :FindFirstChild or anything is nil, it’s the parameter before I have to check? like before :FindFirstChild it’s the character, so I have to make sure the character exists, yeah?

(Testing out your script rn)

Yeah.

Hmm, even tho when I test play with two players at once in studio, both characters havig the same string value, they don’t show up on the player’s list… do you have an idea?

I really appreciate the effort you put in to help me :heart_hands:

Is there only one string value in the character?

Yes, there’s just one string value

Could you see what happens when you use this script?

Code:

local LocalPlayer = game:GetService("Players").LocalPlayer
local InvitePlayer = game:GetService("ReplicatedStorage"):WaitForChild("InvitePlayer")

local function update() -- we create a function which will keep the playerlist updated
	local localCharacter = LocalPlayer.Character
	local animal = localCharacter and localCharacter:FindFirstChildWhichIsA("StringValue")

	if not animal then
		return
	end

	-- Clearing the frame
	for i, v in script.Parent:GetChildren() do -- we loop through the playerlist frame
		if v:IsA("Frame") then -- if the thing we're currently at is a frame then
			v:Destroy() -- we destroy it
		end
	end

	for i, player in game.Players:GetPlayers() do -- we loop through the existing players
		local character = player.Character
		local otherAnimal = character and character:FindFirstChildWhichIsA("StringValue")

		if not otherAnimal or animal.Name ~= otherAnimal.Name then
			continue
		end

		local frame = InvitePlayer:Clone() -- we clone the frame which represents a player and allows us to invite him/her to our crew
		frame.Name = player.Name -- we give it the same name as the player we're currently at
		frame.PlayerName.Text = player.Name -- we give the textlabel inside the frame the same name as the player we're currently at
		frame.Position = UDim2.new(0, 0, 0.02 * i, 0) -- we set its position
		frame.Invite.LocalScript.Disabled = false -- we enable the localscript inside the button 
		frame.Parent = script.Parent -- we put it inside the playerlist frame
	end
end

while task.wait(5) do
	update() -- we call the update function
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.