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

Why is my for loop happening even without touching a brick?

So this is a script for making another part invisible if I step on a brick.

yop = script.Parent.Touched:connect(function()
     yop:disconnect()
	 for i = 0, 1 , 0.01 do 
		game.Workspace.Seed.Transparency = i
		wait() 

	end
	
end)

But when I press play, it disappears without even stepping on it. Why is this? What can I do to fix it?

My new final product in response to LordDragonZord 's comment which is still not working:

script.Parent.Touch:connect(function(hit) if hit.Parent:findFirstChild("Humanoid")then game.Workspace.Seed.Transparency = 1 end end)

1 answer

✓ Accepted answer

#What's the issue? The Touched event runs every time a part touches it. Any part, even the base plate. What you might want to do is check to make sure the humanoid is present then fire the loop.


#Checking for humanoid You can use FindFirstChild. It returns the object by name if it exists. If it doesn't exist it returns nil. We can use that with an if then statement to see if the humanoid exists.

print(workspace:FindFirstChild("Camera"))
print(workspace:FindFirstChild("I don't exist!"))

Camera

nil


#Final Product

local yop = script.Parent.Touched:connect(function(part) --The part that touches script.Parent
	yop:disconnect()
	local humanoid = part.Parent:FindFirstChild("Humanoid") --Humanoid
	if humanoid then --If the humanoid exists then...
		for i = 0,1,0.01 do
			workspace.Seed.Transparency = i --If there is more than one part named "Seed" the script will error!
		end
	end
end)


Hope it helps!

7 comments

0

Great job on figuring out his issue was with the Baseplate.

0

I've been on this site for 2 years, I've seen a LOT of silly mistakes.

0

Thank you so much!

0

The final product you mentioned didn't work. I tried coming up with a new one, and it still didn't work:

0

You have to pre-declare your "yop" variable. Otherwise it doesn't exist at the time your function was declared, meaning you cannot use it from within your function to break the connection. Also, the script wouldn't error if there was more than one "Seed" object in the Workspace; it would simply select the first one that was created.

0

What is pre-declaring?

0

Well I mean they must of updated roblox then because it would of errored back then.

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