Error: "attemping to index local 'player' (a nil value) "
local debounce = true script.Parent.Touched:Connect(function(hit) if debounce == true then print("debounce is turning off") debounce = false local player = game.Players:GetPlayerFromCharacter(hit.Parent) - for i,v in pairs(game.Players:GetPlayers()) do with a for loop if v.Name == player.Name then print("Found player "..v.Name) if v.TeamColor ~= BrickColor.new("Really red") then print("Not in this team") v:LoadCharacter() debounce = true else print("You are on this team") debounce = true end end end end end)
What was the point of using a for loop with :GetPlayers()
to check if the player who touched was in the game when you already got the player by using :GetPlayerFromCharacter
?
local Team = game.Teams.Raider --put team name here local Debounce = false script.Parent.Touched:Connect(function(hit) if Debounce == false then Debounce = true local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then if player.Team == Team then print(player.Name.." is already on Team "..Team.Name) else player.Team = Team print(player.Name.." is now on Team "..Team.Name) end end wait(3) Debounce = false end end)
Please accept my answer if this helped!