generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How would I make an onKeyDown script?

Previous Thread :: Next Thread 
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 02:35 PM
I've looked at the wiki page, but haven't found much. Any tips I should know before I start? I tried, and this is what I got:


local function onKeyDown(e)
x = game.Workspace.FindFirstChild("Humanoid")
x.WalkSpeed = 20
end

 mouse.KeyDown:connect(onKeyDown)
 



A little hopeless :P




~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 02:53 PM
I might have fixed it. Would this work?:



function onKeyDown(key)
key:lower()
if key == e then
script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20
end
 
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
C0D3Y is not online. C0D3Y
Joined: 24 Jul 2010
Total Posts: 1692
26 Jun 2012 02:56 PM
function onKeyDown(key)
k = key:lower()
if k == "e" then
game.Players.LocalPlayer.Character.Humanoid.Walkspeed = 20
end
end
script.Parent.Selected:connect(function(mouse)
mouse.KeyDown:connect(onKeyDown)
end)


That's what I would do. But then again, my tools don't register events for some reason...
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 02:59 PM
Well actually I wanted to know if just putting the script in the Workspace was enough...

What exactly are the steps to make this script work? I have a feeling I have to make a HopperBin and put it in the StarterPack and then put this script inside it. Is that right?

Because I want this to work when a gun is selected.





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 03:03 PM
I have this, a normal script, in a HopperBin in the StarterPack and there is a paintball tool in the starterpack as well. Would it work? Please ignore the --blah's, they are for adding extra uses to other keys later on, but also tell me if the "elseif" won't work :P

function onKeyDown(key)
key:lower()
if key == e then
script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20
elseif key == c then
--blah
elseif key == q then
--blah
elseif key == f then
--blah
elseif key == r then
--blah
end
 end
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
C0D3Y is not online. C0D3Y
Joined: 24 Jul 2010
Total Posts: 1692
26 Jun 2012 03:18 PM
No. Don't think so. Make it so, say,

k = key:lower()

if k == "e" then --Needs to be a string
elseif k == "c" then --You get what I mean
Report Abuse
PurelyRandom is not online. PurelyRandom
Joined: 05 Mar 2011
Total Posts: 36763
26 Jun 2012 03:46 PM
What does

:lower()

do?
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 03:53 PM
I don't exactly know. I was using the wiki, and it said to use :lower() and when I searched :lower(), it didn't come up as a method or an event or anything.





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 03:57 PM
So I fixed the script. It is in a Hopperbin inside the StarterPack, there are no other scripts in the Hopperbin.


function onKeyDown(key)
key:lower()
if key == "e" then
script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20
elseif key == "c" then
--blah
elseif key == "q" then
--blah
elseif key == "f" then
--blah
elseif key == "r" then
--blah
end
 end
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
26 Jun 2012 04:03 PM
lower makes the letter lowercase. Its pretty much the same thing as string.lower
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 04:04 PM
I see....


Anyway, any help on the other thing? :P

This is giving me a headache ​>.>






~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
C0D3Y is not online. C0D3Y
Joined: 24 Jul 2010
Total Posts: 1692
26 Jun 2012 04:04 PM
@ThoseWhoDon'tKnowWhat:lower()Does

It takes the string as a variable,(i.e c = "Character") and puts it in all lowercase. So, say(still using the above axample) you put in to the command bar: print(c:lower()) it would print "character".
Report Abuse
Woodstauk4 is not online. Woodstauk4
Joined: 27 Dec 2010
Total Posts: 3061
26 Jun 2012 04:06 PM
Must be in a Local script tO USE THE KeyDown event

script.Parent.Equipped:connect(function(mouse)
mouse.KeyDown:connect(function(k)
if k=="r"then
game.Players.LocalPlayer:LoadCharacter()end end)end)
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 04:16 PM
@wood


Really :o

A LocalScript inside a Hopperbin inside the StarterPack?





~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
26 Jun 2012 04:25 PM
"Must be in a Local script tO USE THE KeyDown event"

Actually, it must be in a local script to get the mouse.
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 04:27 PM
You people confuse me.

Let me write this down:


I have a Hopperbin inside the StarterGui, and there is a single script in it which contains this:


function onKeyDown(key)
key:lower()
if key == "e" then
script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20
elseif key == "c" then
--blah
elseif key == "q" then
--blah
elseif key == "f" then
--blah
elseif key == "r" then
--blah
end
 end
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)



Is that correct? If not, what do I fix and how do I fix it?



~Leave me in the city with 50 bucks and watch me come back a millionaire~
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
26 Jun 2012 04:51 PM
Looks like it. However, here's a nicer template to use...

    local keyFunctions={
        ["a"]=function()
            --Code
        end;
    }

    script.Parent.Selected:connect(function(mouse)
        mouse.KeyDown:connect(function(key)
            local key=key:lower()
            if keyFunctions[key] then
                keyFunctions[key]()
            end
        end)
    end)
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 04:54 PM
Funny thing is, it didn't. I'll try yours.





~"If I win the lottery, the first thing I'm buying is an optical mouse"~
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 05:13 PM
It didn't work at all. Same setup as before, only the script changed.

local keyFunctions={
    ["a"]=function onKeyDown(e)
        key:lower()
     if key == "e" then
script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20
elseif key == o then
script.Parent.Parent.Parent.PlayerGui.ScreenGui.Frame.Visible = true
end
end
    end;
}

script.Parent.Selected:connect(function(mouse)
    mouse.KeyDown:connect(function(key)
        local key=key:lower()
        if keyFunctions[e] then
            keyFunctions[e]()
        end
    end)
end)





~"If I win the lottery, the first thing I'm buying is an optical mouse"~
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
26 Jun 2012 05:24 PM
you don't understand how it works...

    local player=game.Players.LocalPlayer

    local keyFunctions={
        ["e"]=function()
            player.Character.Humanoid.WalkSpeed = 20
        end;
        ["o"]=function()
            player.PlayerGui.ScreenGui.Frame.Visible = true
        end;
    }

    script.Parent.Selected:connect(function(mouse)
        mouse.KeyDown:connect(function(key)
            local key=key:lower()
            if keyFunctions[key] then
                keyFunctions[key]()
            end
        end)
    end)
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 05:26 PM
Oh my, I see now. So every time I want to add a new function for a key, I would do
 
    ["letterhere"]=function()
        --code
    end;


?



~"If I win the lottery, the first thing I'm buying is an optical mouse"~
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
26 Jun 2012 05:27 PM
You got it.
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 05:29 PM
Alright thanks. By the way, is there a way to make this work for a Paintball gun or any other tool? I find it annoying to have to make people select a Hopperbin JUST so they can select options.




~"If I win the lottery, the first thing I'm buying is an optical mouse"~
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
26 Jun 2012 05:31 PM
You can use that template for anything (as it's only logical). However, you'd need to change the event from .Selected to .Equipped.
Report Abuse
GreenDay987 is not online. GreenDay987
Joined: 21 May 2011
Total Posts: 7027
26 Jun 2012 05:37 PM
Alright. Thanks a bunch.









~"If I win the lottery, the first thing I'm buying is an optical mouse"~
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image