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

KeyDown doesn't work in Online Mode?

I have a LocalScript in StarterGui that detects if the player presses F4. If the player does, it will fire a BindableEvent in ReplicatedStorage, called F4Pressed, which then executes code inside of a server script to mess with some GUIs.

Here's my problem: the server never seems to get any response. There is a print statement in the LocalScript that fires when I press F4, but the server never seems to recognize that the BindableEvent fired.

LocalScript:

player = game.Players.LocalPlayer
mouse = player:GetMouse()

function PressedE(key)
    if key:byte() == 29 and game.ReplicatedStorage.Game.Value == false then
		print(player.Name.." pressed F4.") -- this print statement goes through just fine
        game.ReplicatedStorage.F4Pressed:Fire(player.UserId)
    end
end

mouse.KeyDown:connect(PressedE)

Server Script (snippet):

game.ReplicatedStorage.F4Pressed.Event:connect(function(playerId)
	print("Hello!") -- never fires
end)

1 comment

1

Don't use KeyDown! It's deprecated! Use UserInputService or ContextActionService

1 answer

✓ Accepted answer

BindableEvents don't go across the network.

You need to use RemoteEvents in order to talk between LocalScripts and Scripts.


The code will be almost the same, with a few adjustments:

  • The LocalScript will use :FireServer instead of :Fire
  • The Script will listen to OnServerEvent instead of Event
  • The Script will automatically get the player object as the first parameter, so you don't need to pass player.UserId (since player will be passed automatically) -- instead, the server script can simply inspect player.UserId, where player is its first parameter (instead of playerId)

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