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 do i run a script on a keypress?

Previous Thread :: Next Thread 
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 05:54 PM
Hey, im currently working on some animations but im wondering how would i go about running the animation script? With a keypress. Like X or Z or whatever. I was hoping it wouldnt need a tool but if it does that's totally fine. Any help would be greatly appreciated!

- Ytre12345
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:04 PM
Bumppppppppppppppppppppppp
Report Abuse
Tynexx is not online. Tynexx
Joined: 11 Jul 2012
Total Posts: 1559
17 Jun 2014 06:07 PM
in localscript. StarterGui

player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
--Do stuff
end
end)
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
17 Jun 2014 06:07 PM
You can avoid using tools by using the player:GetMouse() method. The only downside of this is that it has to be done in a Local Script.

Example:

player = game.Players.LocalPlayer

repeat wait(.1) until player.Character

mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
if key == "f" then
print("Omg you pressed f!")
end
end)
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:11 PM
Ok but with

player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
--Do stuff
end
end)

how exactly would it "execute" the script?
Report Abuse
Tynexx is not online. Tynexx
Joined: 11 Jul 2012
Total Posts: 1559
17 Jun 2014 06:14 PM
player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
player.Character.Humanoid.MaxHealth = math.huge
player.Character.Humanoid.Health = math.huge
end
end)

--This is an example
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:18 PM
Ok so lets say my script that i wanted to execute was named "RunAnimation" how would i use that in the code?

player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
run.Script.RunAnimation
end
end)

Or something like that? xD
Report Abuse
Tynexx is not online. Tynexx
Joined: 11 Jul 2012
Total Posts: 1559
17 Jun 2014 06:20 PM
RunAnimation could be a script so, it will be disabled at first then
player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
player.Character.runAnimation.Disabled = false
end
end)
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:21 PM
Ok so this script:

player = game.Players.LocalPlayer
m = player:GetMouse()
m.KeyDown:connect(function(key)
if key:lower == "x" then --The key
player.Character.runAnimation.Disabled = false
end
end)

Will run the "RunAnimation" script?

If so, where would i put the script above?
Report Abuse
Tynexx is not online. Tynexx
Joined: 11 Jul 2012
Total Posts: 1559
17 Jun 2014 06:23 PM
The runAnimation would be in the character.
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:24 PM
With that you mean the folder thing called "Players" right? Where would the other script be? and would it be a LocalScript or Script?
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:33 PM
Buuuump, Could anyone maybe tell me how to use a tool to do this? If it's easier. Using a tool to use a script.
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:45 PM
Buuump pls help D:
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:50 PM
Basically what i want is for this script to run and do the animation in the script when i press the X key

function waitForChild(instance, name, rec)
if not instance:FindFirstChild(name, rec == true) then
repeat wait() until instance:FindFirstChild(name, rec == true)
end
return instance:FindFirstChild(name, rec == true)
end

function waitForProperty(instance, name)
if not instance[name] then
repeat wait() until instance[name]
end
return instance[name]
end

local rs = game:service("RunService").Stepped
local anim = waitForChild(script, "KeyframeSequence")
local play = waitForChild(script, "Play")
local stop = waitForChild(script, "Stop")
local keyr = waitForChild(script, "KeyframeReached")
local loop = waitForChild(script, "EnableLoop")
local model = waitForProperty(script, "Parent")
local fadeOut = false
local steps = {}
local start = tick()
local fadeTime = 0.1
local speed = 1
local oldstep
local mode

local offset = {
["Head"] = {
c0 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(-1, 0, 0), math.pi/2),
c1 = CFrame.new(0, -0.5, 0) * CFrame.fromAxisAngle(Vector3.new(1, 0, 0), -math.pi/2)},
["Torso"] = {
c0 = CFrame.new(0, 0, 0) * CFrame.fromAxisAngle(Vector3.new(-1, 0, 0), math.pi/2),
c1 = CFrame.new(0, 0, 0) * CFrame.fromAxisAngle(Vector3.new(1, 0, 0), -math.pi/2)},
["Left Leg"] = {
c0 = CFrame.new(-0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2),
c1 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)},
["Right Leg"] = {
c0 = CFrame.new(0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2),
c1 = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)},
["Left Arm"] = {
c0 = CFrame.new(-1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2),
c1 = CFrame.new(0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)},
["Right Arm"] = {
c0 = CFrame.new(1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2),
c1 = CFrame.new(-0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)}
}

function sortSteps(obj)
local t, ret = 0, {}
for k, v in next, obj:GetChildren() do
ret[#ret + 1] = {v, t}
t = t + v.Time
end
table.sort(ret, function(a, b) return a[2] < b[2] end)
return ret
end

function jointByPart1(par, part1)
for k, v in next, par:GetChildren() do
if v:IsA("JointInstance") and v.Part1 == part1 then
return v
end
local find = jointByPart1(v, part1)
if find then
return find
end
end
return
end

function allposes(par)
local t = {}
for k, v in next, par:GetChildren() do
if v:IsA("Pose") then
t[tostring(v)] = tostring(v)
end
local other = allposes(v)
for k, v in next, other do
t[tostring(v)] = tostring(v)
end
end
return t
end

function poses(par, assoc)
local t = {}
for k, v in next, par:GetChildren() do
if v:IsA("Pose") then
t[(assoc and tostring(v) or #t + 1)] = v
end
local other = poses(v)
for k, v in next, other do
t[(assoc and tostring(v) or #t + 1)] = v
end
end
return t
end

function lerp(obj, from, to, p)
if offset[tostring(obj)] then
if pcall(function() return game.IsA(from, "Pose") end) then
from = from.CFrame
end
local joint = jointByPart1(model, obj)
local x1, y1, z1 = from:toEulerAnglesXYZ()
local x2, y2, z2 = to:toEulerAnglesXYZ()
joint.C0 = offset[tostring(obj)].c0 * CFrame.new(from.p:Lerp(to.p, p)) * CFrame.Angles(x1 + (x2 - x1) * p, y1 + (y2 - y1) * p, z1 + (z2 - z1) * p)
end
end

rs:connect(function(timer, stepped)
if mode or fadeOut then
local mystep = 0
local perc = 0
local elaps = (tick() - start) * speed
for k, v in next, steps do
if elaps > v[2] and (not steps[k + 1] or (elaps <= steps[k + 1][2])) then
mystep = k
if not fadeOut then
perc = (elaps - v[2]) / (mystep == 1 and speed * fadeTime or (mystep == #steps and (not mode.Loop and speed * fadeTime) or v[1].Time))
else
perc = (elaps - v[2]) / fadeTime
end
end
end
if mystep ~= oldstep then
oldstep = mystep
keyr:Fire(tostring(steps[mystep][1]))
end
if mystep == #steps or fadeOut then
if not fadeOut and mode and mode.Loop then
start = tick()
elseif perc <= 1 then
for k, v in next, allposes(mode) do
if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
lerp(model:FindFirstChild(tostring(v)),
(steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
CFrame.new(),
perc)
end
end
else
for k, v in next, allposes(mode) do
if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
lerp(model:FindFirstChild(tostring(v)),
(steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
CFrame.new(),
1)
end
end
mode = nil
oldstep = nil
fadeOut = false
end
elseif mode then
for k, v in next, poses(steps[mystep][1]) do
if model:FindFirstChild(tostring(v)) and model:FindFirstChild(tostring(v)):IsA("BasePart") and offset[tostring(v)] then
lerp(model:FindFirstChild(tostring(v)),
(steps[mystep - 1] and poses(steps[mystep - 1][1], true)[tostring(v)] or CFrame.new()),
v.CFrame,
perc)
end
end
end
end
end)

play.Event:connect(function(_fadeTime, _speed)
if not mode then
start = tick()
steps = sortSteps(anim)
oldstep = nil
fadeTime = type(_fadeTime) == "number" and _fadeTime or 0.1
speed = type(_speed) == "number" and _speed or 1
mode = anim
end
end)

stop.Event:connect(function()
oldstep = nil
fadeOut = true
repeat
rs:wait()
until not fadeOut
mode = nil
speed = 1
fadeTime = 0.1
-- do stuff?
end)

loop.Event:connect(function(on) -- to skip annoying stuff.
anim.Loop = not not on
end)




Could anyone please help me do that? :D
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 06:58 PM
Buuump
Report Abuse
marcdaro is not online. marcdaro
Joined: 09 Apr 2011
Total Posts: 56
17 Jun 2014 07:09 PM
Have you tried the Tool.Equipped event?
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 07:10 PM
What's that? :O
Report Abuse
marcdaro is not online. marcdaro
Joined: 09 Apr 2011
Total Posts: 56
17 Jun 2014 07:11 PM
It's basically what the Tool does when it is Equipped.
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
17 Jun 2014 07:25 PM
Oh, how do i use that? I want it to basically use that script i posted
Report Abuse
marcdaro is not online. marcdaro
Joined: 09 Apr 2011
Total Posts: 56
17 Jun 2014 09:37 PM
If you are using the Tool action-wise then I would recommend using a Hopperbin, for example
--Put this Script in a Hopperbin
function onKeyDown(key)
key:lower()
if key == "f" then
--script here

end
end

function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
script.Parent.Selected:connect(onSelected)

Try using this.
Report Abuse
Ytre12345 is not online. Ytre12345
Joined: 25 May 2012
Total Posts: 31
18 Jun 2014 07:14 AM
Ok so if the script was named "RunAnimation" do i type it like this:

function onKeyDown(key)
key:lower()
if key == "f" then
--run.Script.RunAnimation

end
end

function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
script.Parent.Selected:connect(onSelected)




Or something like that? Also where would i put the hopperbin and the script?
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