Trying to give users random item when in team playing

This is for a randomizer game im working on but when i use this code it just doesnt work

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		if player and player.TeamColor == game.Teams.Playing.TeamColor then
			for i = 1, 4 do
				local rantool = tools:GetChildren()[math.random(1, #tools:GetChildren())]:Clone()
				rantool.Parent = player.Backpack
				print(player.Name.." got "..rantool.Name)
			end
		end
	end)
end)

Can u specifiy which code line has error???

1 Like

Why are you using a for loop to run it 4 times? is it cause you want to give 4 items? Asking since its unclear

What I mean is that its not giving me the items

Yes I was trying to give 4 items to the player

Check if the if condition even pass through or not,i dont think the if condion pass through

Do you see the print statement being printed in the output or not?

no it doesnt print to the output

then its probably the “if” statement that is returning false. probably because player.TeamColor is not the same as game.Teams.Playing.TeamColor

In these cases I would usually print out any variables I am comparing to see why exactly it doesn’t work.

if player and player.TeamColor == game.Teams.Playing.TeamColor then
	for i = 1, 4 do
		local rantool = tools:GetChildren()[math.random(1, #tools:GetChildren())]:Clone()
		rantool.Parent = player.Backpack
		print(player.Name.." got "..rantool.Name)
	end
else
    print(player.TeamColor)
    print(game.Teams.Playing.TeamColor)
end

If the player team color returns as nil or a default one then from that information I would understand that the code runs before any team has been assigned.

Then I would also be sure if the game.Teams.Playing.TeamColor variable is correct.

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