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

KeyDown doesn't work in Online Mode?

Asked by 8 years ago

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
Don't use KeyDown! It's deprecated! Use UserInputService or ContextActionService Validark 1580 — 8y

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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)
Ad

Answer this question