Team change system not working

I am trying to figure out a system for “blending in” with enemy AI. Currently, their friendly team is set to Really Black, and I need the player to temporarily be on their team, then switch back, using a tool like an invisibility potion.

Here is the script I am using:

local player = game.Players.LocalPlayer

local teams = game:GetService("Teams")

local expiretime = 5

script.Parent.Activated:Connect(function()

script.Parent.Enabled = false

player.Team = teams.Scoobs

player.TeamColor = teams.Scoobs.TeamColor

script.Parent.Part.Heartbeat:Play()

wait(expiretime)

player.Team = teams.Players

player.TeamColor = teams.Players.TeamColor

wait(25)

script.Parent.Enabled = true

end)

I get an error on line 7, please help.

What is the error exactly? Not much we can do to help without the actual error itself.

It doesn’t tell me the error, it just says Line 7. The output window displays something is wrong with line 7.
This line:
player.TeamColor = teams.Scoobs.TeamColor

Can you screenshot the error? Should have some information on it.

Also can you show the full script? Seems some of it got cut off.

output window
of course, only now does the thing decide to actually tell me anything.
script
here’s the full script.

How about you remove the 2 lines from this?

Change something?

This means that whatever you are trying to index with Team, in your case “player” is a nil value. This is in a local script correct?

I saw that part in a tutorial on the developer.roblox.com, I thought it was necessary. It doesn’t change anything.

You can’t use LocalPlayer in a server script, this must be in a localscript to work. Or better yet, since the Tool, when equipped is a child of the character, just do put script.Parent in a variable and when it is activated get the Parent of the tool to get the player’s character, and then use game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)

Here’s what I mean

local tool = script.Parent

local teams = game:GetService("Teams")
local expiretime = 5

tool.Activated:Connect(function()
	local player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
	if not player then return end
	tool.Enabled = false
	player.Team = teams.Scoobs
	player.TeamColor = teams.Scoobs.TeamColor
	tool.Part.Heartbeat:Play()
	wait(expiretime)
	player.Team = teams.Players
	player.TeamColor = teams.Players.TeamColor
	wait(25)
	tool.Enabled = true
end)

Although I’m not sure if you need to put both the Team and the TeamColor as doing one or the other is enough

1 Like

Are you sure that “scoobs” is even a child of Teams?

Oop! It’s not in a LocalScript! Thank you sir!

No problem! You can follow Embat’s solution for this to get the Player Reference in the Server Script.