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

How do I fix this value/touchinterest script?

Asked by
Kitorari 266 Moderation Voter
8 years ago
Sugar = 0
Magicfood = 0
workspace.Partscan.Touched:connect(function(otherpart)
    if otherpart.Name == "sugar" then
        Sugar = Sugar + 1
        print(Sugar)

    end
end)
workspace.Partscan.Touched:connect(function(otherpart)
    if otherpart.Name == "MAGICALFOODMAKER" then
        Magicfood = Magicfood + 1
        print(Sugar)
        print(Magicfood)

    end
end)

if Sugar == 1 and 
    Magicfood == 1 then
     print("You did well you punk")
    else
end

For some reason, Print("You did well you punk") doesn't work, even when the values have been reached.

How can I fix this?

Help is appreciated...

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago
Edited 4 years ago

Problem

When the script gets done knowing it has to listen to events, it will instantly go through the if then statement. Since Magic and Sugar do not equal 1 when the if then statement is reached, it will automatically terminate.


Solution

Put the if then statement in a function and call the function each time you update the Sugar or Magic value.


Final Script

Sugar = 0
Magicfood = 0

function SugarAndMagicCheck() --Establish the function.
    if Sugar == 1 and Magicfood == 1 then --Conditions conditions.
        print("You did well you punk")
    end
end

workspace.Partscan.Touched:connect(function(otherpart)
    if otherpart.Name == "sugar" then
        Sugar = Sugar + 1
        print(Sugar)

        SugarAndMagicCheck() --We're telling the script to go through the function and do it's job.
    end
end)
workspace.Partscan.Touched:connect(function(otherpart)
    if otherpart.Name == "MAGICALFOODMAKER" then
        Magicfood = Magicfood + 1
        print(Sugar)
        print(Magicfood)

        SugarAndMagicCheck() --We're telling the script to go through the function and do it's job.
    end
end)

Just so you know, if the Magic and Sugar values do not both equal 1 the time the function is ran, the if then statement will not run either.


Hopefully this answered your question. If it did, do not forget to hit the accept answer button to the left. If you have any questions, post them in the comments below and I will get back to you.
Ad
Log in to vote
1
Answered by
Ocula 40
8 years ago

You must check the Sugar variable each time it's updated, otherwise all it does is look at the sugar variable once. Try incorporating the conditional that checks the Sugar variable into each .Touched function connection.

0
Thank you! I just shoved it all into one function, so now it works! I probably should clean it up though. Kitorari 266 — 8y
0
No problem, good luck :) Ocula 40 — 8y

Answer this question