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

What am I doing wrong with my for loop?

So this is my script which slowly turns another brick known as "seed" invisible if a player steps on another part.

local seed = game.Workspace.Seed
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then 
		for i= 0,1,-0.01  do
		seed.Transparency = i
    
wait()
	end
end
end)

It works only If I get rid of the for loop, and change it to 'seed.Transparency = 1'. Why is this? Do for loops not work with functions and 'FindFirstChild'?

1 answer

✓ Accepted answer

You need to change your (-0.01 to 0.01) and the wait to (0.01)

finished code=

local seed = game.Workspace.Seed
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")then 
        for i= 0,1,0.01  do
        seed.Transparency = i

wait(0.01)
    end
end
end)

Explanation: So you were making it go up to 1, but in your 3rd argument you were making it go down basically making it useless

The 3rd argument determines if the number goes up, or down

Did this help? Make sure to accept the answer?

Does it not work? Make sure to comment what is wrong and I'll fix it!

(sorry if this is explained terribly)

5 comments

0

Oh! Thank you so much, you explained it perfectly and it worked. One more thing, how do I disconnect this function so I can only make it happen once?

0

I don't think there is a proper function to 'disconnect it

0

Never mind, I found out how now.

0

alright, I didn't finish my answer. You could always use a denounce variable tho. if needed

0

Oh, okay thanks

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