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:
**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.
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.
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)
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)
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:
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)
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?
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?
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