My force field destroy script does not work

Ok, so I’ve been struggling with this problem for awhile now. I am trying to destroy a forcefield upon the player joining a specific team. It will not work no matter what I do, except when I add while wait(0.1) do. I remove it and it just does not want to work. Everything in the script working except after the elseif.
Before making this script, I’ve tried countless other ones but it inevitably led to the same answer.
I am asking for someone to fix my code, and tell me what I have been doing wrong, because this problem is completely out of my knowledge of scripting (not much)
Here is my local script inside of player scripts;

while wait(0.1) do
	local Teams = game:GetService("Teams")
	local player = game:GetService("Players").LocalPlayer
	local char = player.Character or player.CharacterAdded:wait()
	repeat wait() until char:FindFirstChild("Humanoid")
	local human = char.Humanoid
	if player.Team == Teams["Safe"] then
		local forcefield = Instance.new("ForceField",char)
	elseif char:FindFirstChildWhichIsA("ForceField") then char:FindFirstChildWhichIsA("ForceField"):Destroy() 
	end
end
2 Likes

From this topic. No need to scripting.

2 Likes
while wait() do
local Teams = game:GetService("Teams")
for i,v in pairs(game.Players:GetPlayers()) do
local character = v.Character
if character then
if player.Team == Teams["Safe"] then
local ForceField = Instance.new("ForceField")
ForceField.Parent = character
elseif char:FindFirstChildWhichIsA("ForceField") then
char:FindFirstChildWhichIsA("ForceField"):Destroy()
end
end
end

do it in a server script

2 Likes

A ton of force fields still are appearing on the player. I am wondering how to fix this script without a ton of force fields appearing on the player.

1 Like

This is a script to add a force field on a player, and to remove one, depending on the players team.

2 Likes

The script also does not work, I had to edit it a little to fix the errors.

while wait(0.1) do
	local Teams = game:GetService("Teams")
	local player = game:GetService("Players").LocalPlayer
	local char = player.Character or player.CharacterAdded:wait()
	repeat wait() until char:FindFirstChild("Humanoid")
	local human = char.Humanoid
	if player.Team == Teams["Safe"] then
local findFF = char:FindFirstChildWhichIsA("ForceField")
if findFF then findFF:Destroy() end
		local forcefield = Instance.new("ForceField",char)

	elseif char:FindFirstChildWhichIsA("ForceField") then char:FindFirstChildWhichIsA("ForceField"):Destroy() 
	end
end
1 Like

ServerScriptService.Script:4: attempt to index nil with ‘Character’

while wait(0.1) do
	local Teams = game:GetService("Teams")
	local player = game:GetService("Players").LocalPlayer
	local char = player.Character or player.CharacterAdded:wait()
	repeat wait() until char:FindFirstChild("Humanoid")
	local human = char.Humanoid
	if player.Team == Teams["Safe"] then
		local forcefield = Instance.new("ForceField",char)
	else
		if char:FindFirstChildWhichIsA("ForceField") then
			char:FindFirstChildWhichIsA("ForceField"):Destroy() 
		end
	end
end
1 Like

same error sadly :slightly_smiling_face: sorry, my goal is to get this script working without it making alot of new force fields.

1 Like

Sorry I confused myself put this in a server script

local Team = game.Teams

local Safe = Team:FindFirstChild("Safe")

for i,player in pairs(game.Players:GetChildren()) do
	local Character = player.Character
	
	if player.Team == Safe then
		if not Character:FindFirstChild("ForceField") then
			local Field = Instance.new("ForceField",Character)
		end
	else
		if Character:FindFirstChild("ForceField") then
			Character.ForcedField:Destroy()
		end
	end
end
1 Like

the error is happening in line 4 with Character

1 Like

they didn’t identify the variable player do that one by yourself and it should work

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Team == InsertTeamEtc then
			Instance.new("ForceField")
		else
			Character:WaitForChild("ForceField"):Destroy()
		end
	end)
end)

Locate this within a Server Script.

1 Like

but is there a way to make it destroy all of the instances of forcefields instead of doing it 1 at a time ?

no errors, just doesnt work, odd