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

Print isnt working when MouseButton1Down?

Asked by 3 years ago

local air = script.Parent.Air local earth = script.Parent.Earth local fire = script.Parent.Fire local water = script.Parent.Water

if air.MouseButton1Down:Connect() then print "air chosen"

elseif earth.MouseButton1Down:Connect() then print "earth chosen"

elseif fire.MouseButton1Down:Connect() then print "fire chosen"

elseif water.MouseButton1Down:Connect() then print "water chosen" end

I swear im not this bad at coding... im just being stupid and cant solve this.

2 answers

Log in to vote
0
Answered by
zane21225 243 Moderation Voter
3 years ago

There are a ton of mistakes in your code. Let me go over them.

Firstly, it isn't good practice to put all of your variables on a single line of code. Try spacing them out a bit.

Secondly, you should be using WaitForChild if you are trying to reference objects (if your code is running right when the game starts)

For reference:

local air = script.Parent:WaitForChild('Air')
local earth = script.Parent:WaitForChild('Earth')
local fire = script.Parent:WaitForChild('Fire')
local water = script.Parent:WaitForChild('Water')

Thirdly, you don't need all of those If and Elseif statements in your code.

I am going to assume that putting all of those variables into a table would be an easier route for the query that you have, but I am not experienced in the Table world of scripting yet, so I don't want to provide any false information on that.

Anyways, below should be your fixed code (don't forget to include the variables on top.)

air.MouseButton1Down:Connect(function()
print('Air')
end)

earth.MouseButton1Down:Connect(function()
print('Earth')
end)

fire.MouseButton1Down:Connect(function()
print('Fire')
end)

water.MouseButton1Down:Connect(function()
print('Water')
end)

Please let me know if that code works or not. Once again plopping all of those into a table would probably be an easier way, but I am still learning about tables so I am not able to give any help in that field.

Anyways I hope this helps, dear friend. ????

0
I also forgot to state that you had no "ends" in your code, which would also make it not work. Anyways, glad I could help. Like I said before, also please let me know if the code doesn't work because I will be glad to look over it again. It's currently 3 AM where I live so I am definitely tired. Lols zane21225 243 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You don't need an if-statement at all. You have to use a function. Example:

air.MouseButton1Down:Connect(function() print('Air chosen') end)

earth.MouseButton1Down:Connect(function() print('Earth chosen') end)

...etc.

Hope this helps.

Answer this question