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

I have a script that gives a player a forcefield if they are inside a part, why isnt it working?

function onTouch(p)

	local h = p.Parent:FindFirstChild("Humanoid")
	if h then
	
	local ff = p.Parent:findFirstChild("ForceField") 
	
	if h.Health == 100 and not ff then
	
	Instance.new("ForceField", h.Parent)
	
	script.Parent.TouchEnded:connect(function()
		
		if ff then
			
			Instance.new("ForceField", h.Parent):Destroy()
			
		end
		
	end)
	
	end
	end
	end

script.Parent.Touched:connect(onTouch)

can someone please tell me why this isnt working

2 comments

0

Move the TouchEnded function out of the Touch function

0

I'm not new to scripting, i'm just really bad at it. Can you tell me where to put it?

1 answer

✓ Accepted answer
function onTouch(p)

	local h = p.Parent:FindFirstChild("Humanoid")
	
	if h then

		local ff = p.Parent:FindFirstChild("ForceField") 

		if h.Health == 100 and not ff then
			
			local newff = Instance.new("ForceField", p.Parent)

		end
	end
end

function onTouchEnded(p)

	local h = p.Parent:FindFirstChild("Humanoid")

	if h then

		local ff = p.Parent:FindFirstChild("ForceField") 

		if ff then

			ff:Destroy()

		end
	end
end

script.Parent.Touched:Connect(onTouch)
script.Parent.TouchEnded:Connect(onTouchEnded)

The problem was that you destroyed a new ForceField, not the one that the player had. Also, look out for lowercase and uppercase characters, as some functions may not work if not written correctly.

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