My script is giving an error can someone please help

My script is giving an error can someone please help

The script

local p = script.Parent.Parent.Parent.Parent.Parent.Parent.Name
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
	if player:IsInGroup(9392026) then
		game.Players[p].TeamColor = BrickColor.new("Toothpaste")
		game.Workspace[p].Humanoid.Health = 0
		script.Parent.Parent.Parent.Parent.Enabled = false
	end
end)

Output

Players.JadtrugamingYT1.PlayerGui.TeamGUI.Frame.ScrollingFrame.The Royal imperium Guardsman.Script:4: attempt to index nil with 'IsInGroup'

This means that the “Player” variable is equal to nil. You can’t use LocalPlayer in server scripts, so you’ll have to go with a different approach.

this is a normal script not a local script

Local player can only be accessed in Local scrips AKA “local”, try changing to a local script because it seems you’re inside of gui, which would make sense to have a local script for

Ok I found a way and fixed it :smile: :grinning: :smiley: :grinning_face_with_smiling_eyes:

Try using a remote to check the team so everybody can see that the player’s team has changed also LocalPlayer can not be used from a server/normal script.

-- LocalScript inside button
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local button = script.Parent
local remote = ReplicatedStorage:WaitForChild('ChangeTeam')

button.MouseButton1Click:Connect(function()
    remote:FireServer()
end)
-- Serverscript inside ServerScriptService
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remote = ReplicatedStorage:WaitForChild('ChangeTeam')

remote.OnServerEvent:Connect(function(player)
	if player:IsInGroup(9392026) then
		player.TeamColor = BrickColor.new('Toothpaste')
		player:LoadCharacter()
	end
end)