Changing string value based on player's team

The title explains a lot of the problem. I have a string value inside of the character and I want it to constantly update it so that the value = the player’s team. I have looked through all of the dev forum posts about reading a players team but my script does not seem to work. The script is a normal (server) script and it is inside of StarterPlayerScripts.

local playerService = game:GetService("Players")
local Teams = game:GetService("Teams")

playerService.PlayerAdded:Connect(function (player)
	local Team = player.character.Team
	
	while true do
		if player.Team == Teams["Reapers of Valor"] then
			Team.Value = "Reapers of Valor"
		elseif player.Team == Teams["Hostiles"] then
			Team.Value = "Hostiles"
		elseif player.Team == Teams["Spectators"] then
			Team.Value = "Spectators"
		end
		
	wait(0.5)
	end
end)

It looks like it should work, though I think

local Team = player.character.Team

Should be

local Team = player.Character.Team

And your code feels a bit repetitive, you can use GetPropertyChangedSignal on the player’s Team property and make it set the value of t he team value to be the name of the team instead of an infinite loop

Edit since I also forgot to mention this: This will only work once/wont work if the character hasn’t loaded by the time their player did, you need to also use CharacterAdded

you can do something like this

local Players= game:GetService("Players")
local Teams = game:GetService("Teams")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local teamString = player.Character.Team
		plr:GetPropertyChangedSignal("Team"):Connect(function()
			teamString.Value = plr.Team.Name
		end)
	end)
end)

Well your first problem is, your doing while true do. It keeps repeating the first line and the script doesnt stop. You can try using while wait() do. And maybe trying adding a value the becomes true if a player changes teams and if the value is true a script disables and will run this current script.

Thanks, I will change the script around quickly. Also, I didn’t know that you could use a changed event on a teams value so thanks for telling me :smiley: (i have never used the roblox teams system.)

EditL oh okay thanks, i will wait for the the player to load in.

But this is just the harder way. You can use changed events

Will try that as well. Thanks to both of you for giving some of your time to help me.

Okay, thanks to all of you for your time and answers. Brains code worked perfectly! I have been struggling with this problem for about an hour looking through dev forum posts!

Edit: Dont mean to be nit-picky but it needs to me plr instead or player on line 6. Thanks one again for the code, I really appreciate it. :slight_smile:

1 Like

His code is good but I see a problem in it.

Everytime you respawn, you’ll make another connection to listen for when the Team property changes, even though it has already been connected and doesn’t need to be connected again. A simple way would just be to add some code in the listening event to get the team value if possible and update it when the player’s team changes OR you respawn

A simple way would be this

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local function setValue()
		local character = player.Character
		local teamValue = character and character:FindFirstChild("Team")
		if not teamValue then
			return
		end
		teamValue.Value = player.Team and player.Team.Name or ""
	end
	player:GetPropertyChangedSignal("Team"):Connect(setValue)
	player.CharacterAdded:Connect(setValue)
	setValue()
end)

When a player is added, a function gets made for updating the value, and that function is connected to the GetPropertyChangedSignal and the CharacterAdded, and to be safe in the event the character loaded quickly, we make it fire the function immediately as well. This means only 2 events are made instead of the many if you/others respawn a lot.

Also, in the event you are no longer in a team, it’ll just set the value to be an empty string

If you want to have more code in the CharacterAdded instead of just setting the value, just make your own function but make sure at the start to call the setValue function

1 Like