Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago
Edited 8 years ago

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

Log in to vote
1
Answered by
Peeshavee 226 Moderation Voter
8 years ago

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.

0
Nothing has changed, but thank you for the reminder. I could have forgotten! crystalclay 70 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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
0
Thank you for answering my question, but this still didn't work. crystalclay 70 — 8y
0
OH WAIT WE FORGOT A END!! SH_Helper 61 — 8y
0
It should work now, in my mind. SH_Helper 61 — 8y
0
I tried again, and It is still not working. crystalclay 70 — 8y

Answer this question