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

How do I convert this script to a keydown version?

Asked by 6 years ago

I would like to know how to change this current script into one where it functions when you hit the key "r". I can read it but I can't seem to actually write it myself. Can anyone show me how to change it? I will be able to use your changes on future works.

local Tool = script.Parent; 

enabled = true 
function onButton1Down(mouse) --I know this is when the left button of the mouse is clicked
if not enabled then 
return 
end 

enabled = false
script.Parent.Mag.Transparency = 1

wait(3) 
script.Parent.Mag.Transparency = 0

wait(0) 
enabled = true 

end 

function onEquippedLocal(mouse) 

if mouse == nil then 
print("Mouse not found") 
return 
end 

mouse.Icon = "http://www.roblox.com/asset/?version=1&id=1430902" 
mouse.Button1Down:connect(function() onButton1Down(mouse) end) --so as these
end 



Tool.Equipped:connect(onEquippedLocal)

1 answer

Log in to vote
0
Answered by 6 years ago

You don't want to use KeyDown - it is deprecated and less consistent than its newer sibling, UserInputService.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input,GameProcessed)
    if not GameProcessed then -- if the user is not typing
        if Input.KeyCode == Enum.KeyCode.R then -- if the key that got pressed is R
            -- code
        end
    end
end)

Make sure to check the wiki pages on this service as it can appear to be rather complicated and needs documentation for it to be fully understood - especially InputObjects, which is what I defined as Input in the code above.

The problem is, UserInputService only works on LocalScripts, so the current version will not work with FilteringEnabled. This means the code you provided would also have to be in a LocalScript.

If you want, I can provide the FE compatible version, but it will be very confusing at first.


Hope I helped!

~TDP

Ad

Answer this question