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

Why is my touched event script not working with two for loops?

I will try my best to be very descriptive to explain this. I am making a model, in which a player steps on a button (the soil itself) and a seed slowly becomes transparent with a for loop. After the seed is "gone", the player will step on it again and a sprout will slowly appear. Think of the common button you see in games which spawns a car. Instead of a spawn button, the goal of this model is to be an interactive garden/planter. Got that?

This is the code I tried out:

debounce = true
script.Parent.Touched:connect(function(x)
	x.Parent:FindFirstChild("Humanoid")
	if game.Workspace.Seed.Transparency == 0 then debounce = true 
		for i = 0,1,0.01 do
			game.Workspace.Seed.Transparency = i 
			wait(0.1)
			
		end
	end
end)
if game.Workspace.Seed.Transparency == 1 then 
	for m = 1,0,-0.01 do --What I replaced
	game.Workspace.Sprout.Transparency = m --What I replaced
end	

The script doesn't do anything! It was not successful even when I replaced the small section of the code above with Print("Hello"). It also did ** not** work when I replaced it with game.Workspace.Sprout.Transparency = 0 . What is the problem? Does it not accept two for loops or what? What can I do to fix it?

2 answers

I'm new to scripting still, but if I'm not mistaken, you're second for loop needs to have a "-" before 0.01

for m = 1,0,-0.01 do

If you're going to subtract, you should put a negative sign. Hope I helped, and if I didn't, sorry.

1 comment

0

Nothing has changed, but thank you for the reminder. I could have forgotten!

you need TWO equal marks for a if statement. This is incorrect.

if game.Workspace.Seed.Transparency == 0 then debounce = true 

How do we fix it? With a handy thing called common sense! Like this

if game.Workspace.Seed.Transparency == 0 and debounce == true then -- yay it works

But wait, we have to make sure that the touched thing is a player! So we need this.

if hit.Parent:FindFirstChildOfClass("Humanoid") then

Also, in your question you said a property without a equal sign before the value. Just a reminder. So this should be the finished product! Also, I never learned for loops.

local debounce = true
script.Parent.Touched:connect(function(x)
    x.Parent:FindFirstChild("Humanoid")
    if game.Workspace.Seed.Transparency == 0 and debounce == true and x.Parent:FindFirstChildOfClass("Humanoid") then
	local debounce = false --Whats the point of a debounce if it never turns something else!
        for i = 0,1,0.01 do
            game.Workspace.Seed.Transparency = i 
            wait(0.1)
        end
	local debounce = true --Makes it able to work again and not be a junk script
    end
end)
if game.Workspace.Seed.Transparency == 1 then
    for m = 1,0,-0.01 do
    game.Workspace.Sprout.Transparency = m 
	end --Edited
end

4 comments

0

Thank you for answering my question, but this still didn't work.

0

OH WAIT WE FORGOT A END!!

0

It should work now, in my mind.

0

I tried again, and It is still not working.

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