"UserInputService" not working correctly

I get neither “user” output nor “space” output for this code. Where is the mistake?

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
    print("user")
    if input.KeyCode == Enum.KeyCode.Space then
        print("Space")
    end
end)
1 Like

Where is this script and what type of script is it?

1 Like

ServerScriptService and Script

1 Like

Needs to be a local script and somewhere where local scripts run at

1 Like

It looks correct, is the script in StarterCharacterScripts?

Should I use starterplayer or not?

As stated UserInputService can only be ran locally:

" Since this service is client-side only, it will only work when used in a LocalScript or a ModuleScript required by a LocalScript. As UserInputService is client-side only, users in the game can only detect their own input - and not the input of others. "

1 Like

You can, StarterPlayerScripts work.

For example, I want to increase the cash value when I press the space key, I cannot do this via localscript, how should I do it?

Use Remote events to contact the Server from a local script and Handle changing cash on the server

3 Likes

I’m not a programmer, but do you have to put the RemoteEvent on ReplicatedStorage

It depends, most Remotes should be in ReplicatedStorage as it’s replicated on both the server and client and it’s also faster.

The most common place for remote event for developers is Replicated storage. It’s all on developer preference, as long as it exists on the client (player) and the server. (For example, it can’t be in Server storage because it only exists on the Server, however it could be in workspace as workspace is replicated to both the client and the server) it just makes most sense for it to be in Replicated storage but the real reason is preference as long as it exists for the server and client

Mostly people do that, i think its best to actually put your Remotes in replicated storage.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.