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

In this script im getting a error that doesn't let me respawn player and change team. ?

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)

1 comment

0

Why is there a dash on line 8 and why is there 'do with a for loop' on line 9

1 answer

✓ Accepted answer

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!

2 comments

0

^A,lright so, I spotted a error on line 7 "=" needed to be changed to "==" Also, I'm still getting the same error.

0

Updated my answer. Tested this so it will work.

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