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

Moving a GUI by chatting "W", "A", "S", or "D" doesn't work?

Asked by
lucas4114 607 Moderation Voter
8 years ago

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)
0
UPDATED, I accidentally pressed post before I was done, so I had to edit it! Sorry! EzraNehemiah_TF2 3552 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

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


Final Product

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!

0
Same thing happens.... lucas4114 607 — 8y
0
Your parenthesis part is incorrect, there needs to be an equal amount of parenthesis on both sides. (Udim(0.1, 0, 0 ,0) will result in an error because you don't have the second ")" TurboFusion 1821 — 8y
0
It works now! Ty! lucas4114 607 — 8y
0
@TurboFusion, oops! EzraNehemiah_TF2 3552 — 8y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

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

0
Still errors EzraNehemiah_TF2 3552 — 8y
0
Where? dyler3 1510 — 8y

Answer this question