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

How do I fix this value/touchinterest script?

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

✓ Accepted answer

##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.

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.

2 comments

0

Thank you! I just shoved it all into one function, so now it works! I probably should clean it up though.

0

No problem, good luck :)

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