Can't seem to detect if character has been given forcefield

I’ve been trying to make a little script that plays a tween whenever player is given or has forcefield however it does not seem to fire. :frowning: any ideas? Perhaps its the characteradded function?
This is a localscript.

player.CharacterAdded:Connect(function(__CHAR)
	if __CHAR:FindFirstChildOfClass("ForceField") then
		game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 0.2}):Play()
	end
	__CHAR.ChildAdded:Connect(function(__INST)
		if __INST:IsA("ForceField") then
			game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 0.2}):Play()
		end
	end)
	__CHAR.ChildRemoved:Connect(function(__INST)
		if __INST:IsA("ForceField") then
			game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 1}):Play()
		end
	end)
end)

Please keep in mind that a server-script is used to give the player forcefield

Try adding a wait() on the first line within the event connection function (above the if). It may be that the events are being run out of order. If you use the player.CharacterAdded event on the server to put the force field on the character, the client may run the CharacterAdded event before processing the force field.

1 Like

Nope still dosen’t work :frowning:
Also this is how the server event works.

__GIVEFF.OnServerEvent:Connect(function(__PLAY,__WHICH)
	if __WHICH == "On" then
		local __FF = Instance.new("ForceField")
		__FF.Name = "__SHOPFF"
		__FF.Parent = __PLAY.Character
	elseif __WHICH == "Off" then
		if __PLAY.Character:FindFirstChildOfClass("ForceField") then 
			__PLAY.Character:FindFirstChildOfClass("ForceField"):Destroy()
		else
			return
		end
	end
end)

Use print or breakpoint debugging to find which part the code is stuck on. Are you sure the tweening itself works?

Tried debugging and turns out the characteradded function was the problem, why is that?

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait(function(__CHAR)
	if __CHAR:FindFirstChildOfClass("ForceField") then
		print("CHARACTER SPAWNED WITH FF")
		game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 0.2}):Play()
		print("CHARACTER SPAWNED WITH FF REMOVED")
	end
	__CHAR.ChildAdded:Connect(function(__INST)
		if __INST:IsA("ForceField") then
			print("CHARACTER GIVEN FF")
			game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 0.2}):Play()
			print("CHARACTER GIVEN FF REMOVED")
		end
	end)
	__CHAR.ChildRemoved:Connect(function(__INST)
		if __INST:IsA("ForceField") then
			print("CHARACTER REMOVED FF")
			game:GetService("TweenService"):Create(__SHIELDICON, TweenInfo.new(1), {ImageTransparency = 1}):Play()
		end
	end)
end)

You’re using :Wait() instead of :Connect() here.

Well i tried using Conenct too; my last resort in here is to use a fireclient, is it that ideal enough?

put :connect() back and print(“Character Added”) right after so we know if that even works

Are you sure that your CharacterAdded event is being connected at all? Also make sure that it is connected before any character is added. For example, if you put this :

inside a localscript within the character itself, it won’t fire.

Well that didn’t work either…

Then i should probably use a fireclient even

No you don’t need a fire client event. If your localscript is in the character itself, just move it to the StarterPlayerScripts instead of the StarterCharacterScripts.

1 Like