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

KeyPress doesn't work?

Asked by 8 years ago
local uis = game:GetService("UserInputService")


script.Parent.ClickDetector.MouseClick:connect(function(plr)
    local say = 0
    local hud = game.ReplicatedFirst.HUD:Clone()
    hud.TextFrame.Visible = true
    hud.TextFrame.Text.Text = "My mother locks the door when she puts me to sleep..."
    hud.Parent = plr.PlayerGui
    uis.InputBegan:connect(function(inst)
        if inst.KeyCode == Enum.KeyCode.Space then
            if say == 0 then
                hud.TextFrame.Text.Text = "Guess I can't get out this way"
                say = 1
            elseif say == 1 then
                hud:Destroy()
            end
        end
    end)
end)

usual story: Works fine in PlaySolo but not in an actual server. When it comes to a server, the problem doesn't come until the player presses "Space". The GUI pops up on the players screen just fine, however when the player presses "Space", the text on the GUI doesn't change. Also, nothing is printed in the output relating to this script.

All help is appreciated.

1 answer

Log in to vote
2
Answered by
IPsec 35
8 years ago

Recall that UserInputService is meant to be accessed by a LocalScript -- from what you have, it seems like this code may be running inside a regular Script, which is handled by the Server. What you may want to do is something like so:

-- Note: This is meant to be a LocalScript, so it should be put in StarterPack or another place where it will be able to run on any clients immediately.
local uis = game:GetService("UserInputService")
local clickDetector = game.Workspace.ClickableObject.ClickDetector -- Make this variable equal to wherever the ClickDetector is, from Workspace.


clickDetector.MouseClick:connect(function(plr)
    local say = 0
    local hud = game.ReplicatedFirst.HUD:Clone()
    hud.TextFrame.Visible = true
    hud.TextFrame.Text.Text = "My mother locks the door when she puts me to sleep..."
    hud.Parent = plr.PlayerGui
    uis.InputBegan:connect(function(inst)
        if inst.KeyCode == Enum.KeyCode.Space then
            if say == 0 then
                hud.TextFrame.Text.Text = "Guess I can't get out this way"
                say = 1
            elseif say == 1 then
                hud:Destroy()
            end
        end
    end)
end)

The reason it works in Play Solo is because there is no seperation between server and client, because you are serving the place on your local machine. In real games, however, there is a distinct seperation. This is why Server Test can be useful: if something works in Play Solo but not Server Test, it may very well be that there is a similar problem.

Ad

Answer this question