I am making a player list that shows the players’ faction but its not working, the only error is infinite yield. Please help
this is the local script for the faction icon:
local tokyobeasts = "rbxassetid://1632674497"
local lunarkings = "rbxassetid://1632657522"
local markedmen = "rbxassetid://1629001170"
local none = "rbxassetid://1637316912"
local player = game.Players:GetPlayerFromCharacter(game.Workspace:WaitForChild(script.Parent.Parent.Text))
local faction = player:WaitForChild("Data").Faction
print(player.Name)
while true do
wait(2)
if faction.Value == "Lunar Kings" then
script.Parent.Image = lunarkings
end
if faction.Value == "Marked Men" then
script.Parent.Image = markedmen
end
if faction.Value == "Tokyo Beasts" then
script.Parent.Image = lunarkings
end
if faction.Value == "None" then
script.Parent.Image = none
end
end
local player = game.Players:GetPlayerFromCharacter(game.Workspace:WaitForChild(script.Parent.Parent.Text))
local faction = player:WaitForChild("Data").Faction
I guess it’s on line 5 as you’re searching for the player matching the text in script.Parent.Parent.Text. Since this line is only run once at the start of the script, it will search for the player matching whatever is in the TextBox whenever this script is run (which I assume is instantly). So it will most likely be searching for a player with a name matching the default text.
To sort this out, make sure you’re checking the player name in the loop and don’t use WaitForChild because otherwise it will yield until it finds a player matching that name. Here’s what you could use:
local tokyobeasts = "rbxassetid://1632674497"
local lunarkings = "rbxassetid://1632657522"
local markedmen = "rbxassetid://1629001170"
local none = "rbxassetid://1637316912"
print(player.Name)
while wait(2) do
local search = script.Parent.Parent.Text
local player = game.Players:FindFirstChild(search) -- Search players for the name input
if player and player:IsA("Player") then
local faction = player:WaitForChild("Data").Faction
print(plr.Name) -- Should print if player is found
if faction.Value == "Lunar Kings" then
script.Parent.Image = lunarkings
elseif faction.Value == "Marked Men" then
script.Parent.Image = markedmen
elseif faction.Value == "Tokyo Beasts" then
script.Parent.Image = lunarkings
elseif faction.Value == "None" then
script.Parent.Image = none
end
end
end
Edit: I forgot to remove the yielding lines, I’ve updated it now.
Try putting the print before the faction variable declaration, if it prints then then the yield is on the faction line. If this is the case, make sure that the path is actually valid.
The infinite yield warning would come from one of your :WaitForChild calls. This would may mean the object youre looking for never gets created. It looks like you’re searching for a player’s name that is inputted in a text box.
I’m not sure if this player is always the player’s character, but I’ve posted some code to get you started below that will always query the local player’s character.
It looks like this loop is being used to poll for the newest value in a StringValue. This would be a good case to leverage Events. StringValues have an event labeled Changed. When the property “Value” changes, it will fire this event with the function assigned to it and also pass the new property as an argument.
You can read more about events and the parameters they carry on the wiki.
local Images = {
["Tokyo Beats"] = "rbxassetid://1632674497",
["Lunar Kings"] = "rbxassetid://1632657522",
["Marked Men"] = "rbxassetid://1629001170",
["None"] = "rbxassetid://1637316912",
}
local Players = game:GetService("Players")
--If we're running with a local script, Players.LocalPlayer (a property) will be initalized)
local localPlayer = Players.LocalPlayer
local data = player:WaitForChild("Data")
local factionValue = data:WaitForChild("Faction")
factionValue.Changed:Connect(function(newValue)
script.Parent.Image = Images[newValue]
end)
If you want to do something similar for all players in the server, you’ll likely want to start listening to these connections has new players join.
You can achieve this effect with the following code (note this LocalScript would likely not live inside of the buttons like before but rather higher up the chain like the buttons’ parent or so)
--LocalScript This assumes the script is located at the top level container of your ui
local Players = game:GetService("Players")
--
local Images = {
["Tokyo Beats"] = "rbxassetid://1632674497",
["Lunar Kings"] = "rbxassetid://1632657522",
["Marked Men"] = "rbxassetid://1629001170",
["None"] = "rbxassetid://1637316912",
}
local function onPlayerAdded(newPlayer)
--assuming this Data folder is made by a different script
local data = newPlayer:WaitForChild("Data")
local factionValue = data:WaitForChild("Faction")
--we can run the same code as before.
factionValue.Changed:Connect(function(newValue)
-- it looks like the old script had a LocalScript in every gui object.
-- We'll have to search for the right one this time
-- We can use the same dictonary as we did before.
local imageLabel = --somehow get this label.
imageLabel.Image = Images[newValue]
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
--just in case we missed someone coming in from the event (happens in PlaySolo)
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end