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

Why does this UserInputService script not work? shows nothing in output window.

Asked by 2 years ago

So i'm using a proximity prompt to make a player sit in a vehicle seat.

This is the Sit script

local proximityPromt = script.Parent
local seat = proximityPromt.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function() 
    if seat.Occupant then
        proximityPromt.Enabled = false
    else
        proximityPromt.Enabled = true
    end
end)

proximityPromt.Triggered:Connect(function(player)
    seat:Sit(player.Character.Humanoid)
end)

The UnSit script is inside the vehicle seat not the prompt.

local Players = game:GetService("Players")
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

    if input.UserInputType == Enum.UserInputType.Keyboard then

        if input.KeyCode == Enum.KeyCode.G then
            Players.Character.Humanoid.Sit = false
        end

    end 
end)

It supposed to make the humanoid not sit anymore after you press G. But it doesn't work and there is nothing in the output window. I tried using a local script, tried adding the code to the sit script it just doesn't work, Nothing happens when i press the keycode.

0
UserInputService only works on the client and local scripts don't run in the workspace. Change the script to a local script and move the local script somewhere other than workspace. You can see all the different places where local scripts can run here: https://developer.roblox.com/en-us/api-reference/class/LocalScript xInfinityBear 1777 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

UserInputService does not work on server scripts. You will need to listen for a key press on a LocalScript and tell the server to kick the character from its seat using a RemoteEvent

First, put a RemoteEvent in ReplicatedStorage, where both the server and the client can see it

LocalScript:

local userInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

    if input.UserInputType == Enum.UserInputType.Keyboard then

        if input.KeyCode == Enum.KeyCode.G then
    -- tell any server scripts that are listening to a RemoteEvent called "GetUpFromSeat" to run
            ReplicatedStorage.GetUpFromSeat:FireServer() 
        end

    end 
end)

Server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.GetUpFromSeat.OnServerEvent:Connect(function(player)
    -- when you fire a RemoteEvent from a client, the player who fired it is passed to the server by Roblox automatically
    player.Character.Humanoid.Sit = false
end)
Ad

Answer this question