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

How can i make this constant?

Asked by 8 years ago

I want to make it so when you press d and hold it it keeps on adding 15 and moving 15 up Idk how to do it, any help?

Mouse.KeyDown:connect(function(key)
key = key:lower()
if key == d' then
Heart.Position = Heart.Position + UDim2.new(0.0, 15, 0.0, 0)    
end
end)

2 answers

Log in to vote
2
Answered by 8 years ago

Do the following:

local down = false -- variable used to see if the key is down

Mouse.KeyDown:connect(function(key)
    key = key:lower()
    if key == 'd' then
        down = true -- starts moving
    end
end)

Mouse.KeyUp:connect(function(key)
    key = key:lower()
    if key == 'd' then
        down = false -- stops moving
    end
end)

while wait(0.1) do -- how long it waits in between movements
    if down == true then
        Heart.Position = Heart.Position + UDim2.new(0.0, 15, 0.0, 0) -- moves it
    end
end

Please note that KeyDown/Up are deprecated, and can be removed at any time, so you should consider using UserInputService instead.

Hope I helped :)

~TDP

0
Since this is GUI code, it's better to use RenderStepped instead of wait() adark 5487 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

It would probably be something like this:

local dDown = true --Define Variable

Mouse.KeyDown:connect(function(key) --KeyDown Function
key = key:lower()
if key == "d" then
dDown = false
end
end)

Mouse.KeyUp:connect(function(key) --KeyUp Function
key = key:lower()
if key == "d" then
dDown = false
end
end)

while wait() do
if dDown == true then --Declare If Statement
Heart.Position = Heart.Position + UDim2.new(0.0, 15, 0.0, 0)    
end
end
0
Damn it! UniversalDreams 205 — 8y

Answer this question