Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is 'player' being indexed as a nil value?

I want a player to be teleported away from a part if their team color doesn't match the part's color. Output says there is a problem with line 4, can anyone help me?

script.Parent.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	wait()
	if player.TeamColor == BrickColor.new("Bright blue") or player.TeamColor == BrickColor.new("Really blue") and script.Parent.BrickColor == BrickColor.new("Bright green") then
		hit.Parent.Torso.CFrame = CFrame.new(Vector3.new(-49,4,109))
	elseif player.TeamColor == BrickColor.new("Bright green") or player.TeamColor == BrickColor.new("Camo") and script.Parent.BrickColor == BrickColor.new("Bright blue") then
		hit.Parent.Torso.CFrame = CFrame.new(Vector3.new(-49,4,-79))
	end
end)

1 comment

0

The block is teleporting me no matter what team color I possess, which is the problem.

2 answers

✓ Accepted answer

Try making "local player = " to "local player = game.Players.LocalPlayer" , add a wait(2) at line 1 and make it a local script

script.Parent.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    wait()
    if player.TeamColor.BrickColor == BrickColor.new("Bright blue") or player.TeamColor.BrickColor == BrickColor.new("Really blue") and script.Parent.BrickColor == BrickColor.new("Bright green") then
        hit.Parent.Torso.CFrame = CFrame.new(Vector3.new(-49,4,109))
    elseif player.TeamColor.BrickColor == BrickColor.new("Bright green") or player.TeamColor.BrickColor == BrickColor.new("Camo") and script.Parent.BrickColor == BrickColor.new("Bright blue") then
        hit.Parent.Torso.CFrame = CFrame.new(Vector3.new(-49,4,-79))
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.