Not assigning teams

I have a script that automatically assigns teams on join
there is a value given to the player that defaults to 1 if no data is found
there is another value inside the player that stores a string and its value is the player’s team name

there is a modulescript being used to keep track of teams
every element of the modulescript is a team using

game:GetService("Teams"):WaitForChild("teamname")

Output:
12: Attempt to index nil with ‘name’

local ranks = require(script.Parent:WaitForChild("Ranks")

function setteam(player: Player)
local rank: IntValue = player:WaitForChild("Rank")
if rank.Value <= 10 then
player.Team = ranks[rank.Value]
else
player.Team = ranks[11]
end
local rankname: StringValue = player:WaitForChild("leaderstats"):WaitForChild("Rank")
local team: Team? = player.Team
rankname.Value = tostring(team.Name)
end

game.Players.PlayerAdded:Connect(function(player: Player)
setteam(player)
player:WaitForChild("Rank").Changed:Connect(function()
setteam(player)
end)
end)
2 Likes

Hay there let me read throughout your fourm please gimie a few minutes

Most likely, somewhere there is an error and the team is not assigned to the player, or it is nil. Send your Ranks module please.

local ranks = {}
ranks[1] = game:GetService("Teams"):WaitForChild("Citizen");
ranks[2] = game:GetService("Teams"):WaitForChild("Private");
ranks[3] = game:GetService("Teams"):WaitForChild("Corporal");
ranks[4] = game:GetService("Teams"):WaitForChild("Sergeant");
ranks[5] = game:GetService("Teams"):WaitForChild("Colonel");
ranks[6] = game:GetService("Teams"):WaitForChild("Lieutenant");
ranks[7] = game:GetService("Teams"):WaitForChild("Captain");
ranks[8] = game:GetService("Teams"):WaitForChild("General");
ranks[9] = game:GetService("Teams"):WaitForChild("Second Commanding Officer");
ranks[10] = game:GetService("Teams"):WaitForChild("First Commanding Officer");
ranks[11] = game:GetService("Teams"):WaitForChild("Training Officer");

return ranks

Perhaps you have something wrong with the value (player:WaitForChild("Rank")) (is it <=0)? Because it works for me.

default value is 1
i printed my value in-game and it printed 1 so it cant be

Try this script and send your output.

local ranks = require(script.Parent:WaitForChild("Ranks"))

local function setteam(player: Player)
	local rank: IntValue = player:WaitForChild("Rank")
	print(rank.Value)
	if rank.Value <= 10 then
		print(ranks[rank.Value])
		player.Team = ranks[rank.Value]
	else
		print(ranks[11])
		player.Team = ranks[11]
	end
	local rankname: StringValue = player:WaitForChild("leaderstats"):WaitForChild("Rank")
	local team: Team? = player.Team
	print(team)
	rankname.Value = tostring(team.Name)
end

game.Players.PlayerAdded:Connect(function(player: Player)
	setteam(player)
	player:WaitForChild("Rank").Changed:Connect(function()
		setteam(player)
	end)
end)

0
nil
nil

(pleaseremove30characterrule)

after posting, i realised
why the heck is it 0, i printed another time and was 1

Okay, your value (player:WaitForChild("Rank")) is 0. Check again, you’re setting the wrong value.

Send the script where you set the value.

ehhhh


it’s correct, but script thinks its 0

Perhaps the script accepts the value too early (before it was set). This may be due to the DataStore or other functions, please send a script or set the default value to 1. Example:

game.Players.PlayerAdded:Connect(function(player)
	local rank = Instance.new("IntValue", player)
	rank.Value = 1
	rank.Name = "Rank"
	...
end)

could I make a new boolvalue in player set to false and when data is loaded, it becomes true?
inside the script I could add

repeat task.wait() until player:WaitForChild("DataLoaded").Value == true

or screw that and add:

repeat task.wait() until rank.Value ~= 0

Yes, you can. But I do not recommend the first option.

1 Like

oh and script:


i cannot believe the solution was that simple

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