Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

10 comments

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

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.

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.

0

Your script might be disabled or something...?

0

How would I check? I haven't disabled anything. It's under a folder, under a VehicleSeat if that helps.

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)

0

Found something out: server scripts under seats only run if there is an occupant. Lemme try something

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.

0

Sounds good. Mind accepting my answer?

0

Oh my god, sorry, I totally forgot... Anyways, I accepted it, hope you don't mind my 3 month delay

This discussion is preserved as it was. New answers, comments, and votes are closed.