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

Cannot get key input to work?

Asked by
AshJack 15
10 years ago

Hi all. I have been trying to get key input to work for my game, where the user controls the camera height with w/s or up arrow/down arrow. However, it is not working. Nothing happens at all. It is a localscript

Here is my code:

local cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local camball
local posspeed = 1.1
local negspeed = 0.9
local mouse = player:GetMouse()
local keys = {}



if workspace.Lots.Lot1.Value == player.Name then
cam.CameraSubject=workspace.CamBall1
camball = workspace.CamBall1
cam.CameraType = "Attach"
print(camball.Name) --This prints the name fine.
elseif workspace.Lots.Lot2.Value == player.Name then
cam.CameraSubject=workspace.CamBall2
camball = workspace.CamBall2
cam.CameraType = "Attach"
print(camball.Name)--This prints the name fine.
elseif workspace.Lots.Lot3.Value == player.Name then
cam.CameraSubject=workspace.CamBall3
camball = workspace.CamBall3
cam.CameraType = "Attach"
print(camball.Name)--This prints the name fine.
elseif workspace.Lots.Lot4.Value == player.Name then
cam.CameraSubject=workspace.CamBall4
camball = workspace.CamBall4
cam.CameraType = "Attach"
print(camball.Name)--This prints the name fine.
elseif workspace.Lots.Lot5.Value == player.Name then
cam.CameraSubject=workspace.CamBall5
camball = workspace.CamBall5
cam.CameraType = "Attach"
print(camball.Name)--This prints the name fine.
elseif workspace.Lots.Lot6.Value == player.Name then
cam.CameraSubject=workspace.CamBall6
camball = workspace.CamBall6
cam.CameraType = "Attach"
print(camball.Name)--This prints the name fine.
end


mouse.KeyDown:connect(function(key) keys[key] = true


if keys == keys["w"] or keys["\x11"] then
print("I actually do something")--This does NOT print anything at all
camball.Position.Y = camball.Position.Y * posspeed
elseif keys == keys["s"] or keys["\x12"] then
print("I actually do something too")--This does NOT print anything at all
camball.Position.Y = camball.Position.Y * negspeed
end
end)

mouse.KeyDown:connect(function(key) keys[key] = true

if keys == keys["w"] or keys["\x11"] then
posspeed = posspeed + 0.1
--negspeed = 1 - (posspeed - 1)
elseif keys == keys["s"] or keys["\x12"] then
negspeed = negspeed - 0.1
--posspeed = 1 + (1 - negspeed)
else
wait(1)
if keys == not keys["w"] or keys == not keys["\x11"] or keys == not keys["s"] or keys == not keys["\x12"] then
negspeed = 0.9
posspeed = 1.1
end
end
end)

coroutine.resume(coroutine.new(function()
    while wait() do
        cam.CoordinateFrame = CFrame.new(camball.Position) * CFrame.new(0, 0, -30)
    end
end))

I then get this error:

19:57:55.427 - Workspace.Player1.LocalScript:73: attempt to call field 'new' (a nil value)
19:57:55.427 - Stack Begin
19:57:55.433 - Script 'Workspace.Player1.LocalScript', Line 73
19:57:55.433 - Stack End

0
Do tab you code correctly, it makes it much easier to read especially with large amount of code. You're lucky you got answers at all. Perci1 4988 — 10y

3 answers

Log in to vote
1
Answered by
Scubadoo2 115
10 years ago

What I think is happening is that it is stuck on this line of code at line 45

while wait() do

cam.CoordinateFrame = CFrame.new(camball.Position) * CFrame.new(0, 0, -30)
end

You don't have anything to break that loop, so it is stuck doing that continually, never getting to the rest of the script where you hook up the mouse.KeyDown event. Therefore it never is hooked up to begin with.

My suggestion: just put the loop at the end of the script, that way it goes through everything and THEN starts the action.

0
i tried it but it still did nothing AshJack 15 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

Adding on to what Scubadoo2 said about how the loop never breaks, you could just instead wrap the while true do in a coroutine so that it runs at the same time without causing problems:

coroutine.resume(coroutine.new(function()
    while wait() do
        cam.CoordinateFrame = CFrame.new(camball.Position) * CFrame.new(0, 0, -30)
    end
end))

Try this and see how it goes, if it doesn't help try giving output or other information that may help us assist you.

Log in to vote
0
Answered by
AshJack 15
10 years ago

I did what you said, zach

0
Are you sure that the output is correct? If so it's saying line 73 is where the error is even though on that line it has a coroutine.. VariadicFunction 335 — 10y

Answer this question