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

Why doesn't detecting keyboard input doesn't work?

Asked by 4 years ago

I have this code:

local mainServer  = script.Parent.MainServer
local accFwdEvent = mainServer.accelerateForward
local accBwdEvent = mainServer.accelerateBackward

local player      = game.Players.LocalPlayer -- Get player
local mouse       = player:GetMouse() -- Get mouse

local function accelForward()
    accFwdEvent:FireServer(true, 1)
    print("Sent!")
end
local function accelBackward()
    accBwdEvent:FireServer(false, 1)
end

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.W then 
        accelForward()
    end 
end)

I am pressing W, but I don't see the "Sent!" I should. My RemoteEvents and everything is set up properly. I've tried all ways I've seen. NOTHING works. This is in a localscript. Please help.

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
4 years ago

Your remote events and server script most likely aren't running in the right places. It seems to be looking like you've parented your serverScript and your remoteEvents together, and they seem to be running on the client.

--local script
Here's what your hierarchy should look like:

local replicatedStorage = game:GetService("ReplicatedStorage")  

local accFwdEvent = replicatedStorage.accFwd
local accBwdEvent = replicatedStorage.accBwd

local player = game.Players.LocalPlayer -- Get player
local mouse = player:GetMouse() -- Get mouse

local function accelForward()
    accFwdEvent:FireServer(true, 1)
    print("Sent!")
end
local function accelBackward()
    accBwdEvent:FireServer(false, 1)
end

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.W then 
        accelForward()
    end 
end)

We use replicatedStorage because the client and the server can both "see" them/have access to them.

--server script, assuming this is your "mainServer"
local replicatedStorage = game:GetService("ReplicatedStorage")

local accFwdEvent = replicatedStorage.accFwd
local accBwdEvent = replicatedStorage.accBwd

local function printIfConnected()
    print("hey you got it right now")
end

accFwdEvent.OnServerEvent:Connect(printIfConnected)
0
I just changed the hierarchy up based on what I have to assume is wrong, all the code looked absolutely correct from you and I didn't have to change anything but that. If this didn't help, my bad lol Psudar 882 — 4y
0
This would work if multiple instances of the same localscript fired to the events, right? I am making a car chassis, and I know that player is the first passed argument on an event. iSpaceRBLX 31 — 4y
0
I just noticed, that no keystrokes work, anywhere. Even printing on the client from a key press doesn't work. I'll try to publish the place to see if it works. iSpaceRBLX 31 — 4y
0
Your script might be disabled or something...? Psudar 882 — 4y
View all comments (6 more)
0
How would I check? I haven't disabled anything. It's under a folder, under a VehicleSeat if that helps. iSpaceRBLX 31 — 4y
0
Nope, doesn't work anywhere. Even putting the script outside of the vehicleseat. Still nothing if I just print() on a keypress (any keypress shall i mention) iSpaceRBLX 31 — 4y
0
Found something out: server scripts under seats only run if there is an occupant. Lemme try something iSpaceRBLX 31 — 4y
1
I got it to work, somehow. I just used the first result from typing "shift to sprint" into the toolbox. I used the importer and made it import in my character. That only runs when I sit. Wonderful. Then, I changed up the scripts. iSpaceRBLX 31 — 4y
0
Sounds good. Mind accepting my answer? Psudar 882 — 4y
0
Oh my god, sorry, I totally forgot... Anyways, I accepted it, hope you don't mind my *3 month delay* iSpaceRBLX 31 — 4y
Ad

Answer this question