So, I made this script to move a ImageLabel around up, down, left or right on what the player says. But what happens is that it moves the GUI up by -0.1 no matter if the player says "W", "A", "S" or "D" or even anything else like "Hi" or anything.... Help me please? I try changing stuff and the output says nothing but I can't fix it... :c
function Move(msg) if msg == "w" or "W" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, -0.1, 0)) elseif msg == "a" or "A" then script.Parent.Position = script.Parent.Position + (UDim2.new (-0.1, 0, 0, 0)) elseif msg == "s" or "S" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, 0.1, 0)) elseif msg == "d" or "D" then script.Parent.Position = script.Parent.Position + (UDim2.new (0.1, 0, 0, 0)) end end script.Parent.Parent.Parent.Parent.Chatted:connect(Move)
The problem is that you aren't closing off the parentheses:
(There is nothing wrong with this statement
or (still nothing wrong))
Just delete the excess parentheses
function Move(msg) if msg == "w" or "W" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, -0.1, 0) elseif msg == "a" or "A" then script.Parent.Position = script.Parent.Position + (UDim2.new (-0.1, 0, 0, 0) elseif msg == "s" or "S" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, 0.1, 0) elseif msg == "d" or "D" then script.Parent.Position = script.Parent.Position + (UDim2.new (0.1, 0, 0, 0) end end script.Parent.Parent.Parent.Parent.Chatted:connect(Move)
The Second problem is your if statements. You have an or there, it's going to always move down -0.1 no matter what. Since it is basically saying: "Press lowercase w to do this, or if 'W' is real then..." W is real, or not nil, so it will always run! To solve this just add msg ==
in front of "or", but it might make the script seem too long, instead we will say: "If the lowercased version of msg is 'w' then..." We will be using string.lower()
local text = "HI" print(text:lower()) print(string.lower(text)) --Both say "hi", so it turns "HI" into "hi".
script.Parent.Parent.Parent.Parent.Chatted:connect(function(msg) --Anonymous functions :3 if msg:lower() == "w" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, -0.1, 0) elseif msg:lower() == "a" then script.Parent.Position = script.Parent.Position + (UDim2.new (-0.1, 0, 0, 0) elseif msg:lower() == "s" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, 0.1, 0) elseif msg:lower() == "d" then script.Parent.Position = script.Parent.Position + (UDim2.new (0.1, 0, 0, 0) end end)
Hope it helps!
Your problem here lies within your if statements. You need to completely redefine each piece before or else it won't work correctly. You had this for each one:
if msg == "w" or "W" then
You need to redefine the first part like this:
if msg == "w" or msg = ="W" then
The way you had it, the game was checking to see if "W" was true, so it automatically fired the first time on W, which makes it go up.
Now to implement the fixed script. I went through and fixed all of these for you, so here ya go:
function Move(msg) if msg == "w" or msg == "W" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, -0.1, 0)) elseif msg == "a" or msg == "A" then script.Parent.Position = script.Parent.Position + (UDim2.new (-0.1, 0, 0, 0)) elseif msg == "s" or msg == "S" then script.Parent.Position = script.Parent.Position + (UDim2.new (0, 0, 0.1, 0)) elseif msg == "d" or msg == "D" then script.Parent.Position = script.Parent.Position + (UDim2.new (0.1, 0, 0, 0)) end end script.Parent.Parent.Parent.Parent.Chatted:connect(Move)
Also, you could just do this for checking each:
if msg:lower() == "w" then
This isn't required, but it's simpler than your method. Both work however
Anyways, the script should work now. Hope I helped, and if you have any further questions/problems please leave a comment below :P