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

Keybind script in a vehicle doesn't work. Help?

Asked by 1 year ago
Edited 1 year ago

I'm currently trying to make a system where you an set the direction with Keybinds: R (Forward) F (Neutral) and V (Reserve) Nothing happens. I tried doing print("Test") but It didn't work either.

I'll preferably use normal script but I aslo tried it with local script.

Can anyone help me?

local UIS = game:GetService("UserInputService")
local seat = script.Parent   
local F = script.Parent.Forward
local N = script.Parent.Neutral
local R = script.Parent.Reverse


while seat.Occupant ~= nil do
    UIS.InputBegan:Connect(function(input, gameProcessedEvent)      
        if input.UserInputType == Enum.UserInputType.Keyboard then 
            local key = input.KeyCode                               
            if key == Enum.KeyCode.R then                   
                if F == false then
                    F.Value = true
                    R.Value = false
                    N.Value = false
                else
                    F.Value = false
                    R.Value = false
                    N.Value = true
                end

            elseif key == Enum.KeyCode.V then
                F.Value = false
                R.Value = true
                N.Value = false
            else
                F.Value = false
                R.Value = false
                N.Value = true
            end

        elseif input.KeyCode == Enum.KeyCode.F then
            N.Value = true
            F.Value = false
            R.Value = false

        end
    end)

    wait(0.1)
end

Thanks in advance

2 answers

Log in to vote
0
Answered by
ultrabug 306 Moderation Voter
1 year ago
Edited 1 year ago

The UserInputService actually is only client side so you will have to use a localscript to get the user input. You can send this information from the client to the server with remotefunctions or remoteevents, and then go from there.

Here's something I threw together as kind of an example of how you could send the input across:

Local Script in StarterPlayerScripts

local UIS = game:GetService("UserInputService")
local seat = game.Workspace:WaitForChild("Seat")
local event = seat.RemoteEvent
local players = game:GetService("Players")
local keycode = Enum.KeyCode
local keys = {keycode.F, keycode.N, keycode.R}

--Send info when input begins
UIS.InputBegan:connect(function(input, processed)--Get input
    if seat.Occupant == players.LocalPlayer.Character.Humanoid then --check if the input should be sent
        if input.UserInputType == Enum.UserInputType.Keyboard then --check if the input is the right type
            for i,v in ipairs(keys) do --find out if the input is one of the right keys
                if input.KeyCode == v then
                    event:FireServer(input.KeyCode, "began") --trigger the event for the server
                end
            end
        end
    end
end)
--Send info when input ends
UIS.InputEnded:connect(function(input, processed)
    if seat.Occupant == players.LocalPlayer.Character.Humanoid then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            for i,v in ipairs(keys) do
                if input.KeyCode == v then
                    event:FireServer(input.KeyCode, "ended")
                end
            end
        end
    end
end)

Script inside the seat

local event = script.Parent.RemoteEvent
local forward = false
local reverse = false
local neutral = false
function handleinput(player, input, inputtype: string)
    print(player.Name.." "..inputtype.." "..tostring(input))
    if input = Enum.KeyCode.F then
        if inputtype = "began" then
            forward = true
        else
            forward = false
        end
    elseif input = Enum.KeyCode.R then
        if inputtype = "began" then
            reverse = true
        else
            reverse = false
        end
    else
        if inputtype = "began" then
            neutral = true
        else
            neutral = false
        end
    end
end

event.OnServerEvent:Connect(handleinput)

From these I bet you could figure out a way to make it work with what you had going, hopefully this will help!

Edit: I edited the script portion to show you how you could setup your bool values. I haven't run this myself and typed it directly in here so hopefully I didn't mistype anything.

0
Thanks but how do I make it activate a bool value when pressing the key? Edguy2501 7 — 1y
0
Instead of printing in the script inside the seat you can have it set your bool values, and then set them to false when the key gets released again. I'll edit my answer so you can see what I mean. ultrabug 306 — 1y
0
That somehow doesn't work either, even as a local script. Edguy2501 7 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Ok I tried this as a normal script and as a local script, both of them didn't work.

local event = script.Parent.RemoteEvent
local forward = false
local reverse = false
local neutral = false
function handleinput(player, input, inputtype: string)
    print(player.Name.." "..inputtype.." "..tostring(input))
    if input == Enum.KeyCode.R then
        if inputtype == "began" then
            forward = true
        else
            forward = false
        end
    elseif input == Enum.KeyCode.V then
        if inputtype == "began" then
            reverse = true
        else
            reverse = false
        end
    elseif input == Enum.KeyCode.F then
        if inputtype == "began" then
            neutral = true
        else
            neutral = false
        end
    end
end

event.OnServerEvent:Connect(handleinput)

local F = script.Parent.Forward
local N = script.Parent.Neutral
local R = script.Parent.Reverse

if forward == true then
    F.Value = true
elseif forward == false then
    F.Value = false
end

if neutral == true then
    N.Value = true
elseif forward == false then
    N.Value = false
end

if reverse == true then
    R.Value = true
elseif forward == false then
    R.Value = false
end
0
This script should be a normal script, not local. Do you have the local script firing the remoteevent? If you do then the next problem I see it that when you set the values in the lower section you only set them once when the script first runs, so you should wrap that section in a while loop. I would probably only run the loop while the seat has an occupant. ultrabug 306 — 1y
0
You could also just put the part where you change the values inside of the handleinput function instead of doing it separately. ultrabug 306 — 1y
0
I got it!!!! Thank you very much. This helped me a lot. Edguy2501 7 — 1y

Answer this question