Launch a torpedo when a player presses a button while seated

Hello, Im making a script to launch a torpedo from a boat when the player presses the E key.
Currently, im using a script within the seat to tell what character is in the seat. This fires an event forwarding the character name and seat.

These are connected to a local script in the backpack, as I heard that you can only check for user inputs on the client.

My problem is that I cannot figure out how to implement the player pressing “E” into destroying the weld holding the torpedo on.
I know how to detect the player pressing E using UIS, but dont know how to translate that input into destroying the weld between the vehicle and the torpedo, for only the vehicle the player is seated in.

Any help is appreciated greatly.

Current scripts:
IsSomeoneInTheSeat? (Child of seat)

local function fireevent()
if script.Parent.Occupant ~= nil then
print(script.Parent.Occupant.Parent.Name) game.ReplicatedStorage.SittingInABoat:Fire(script.Parent.Occupant.Parent.Name, script.Parent) – brings name of character, and seat through the bindableEvent
end
end
script.Parent:GetPropertyChangedSignal(“Occupant”):Connect(fireevent)

LocalScript (located in StarterPack)

local UIS = game:GetService(“UserInputService”)
local Players = game:GetService(“Players”)
local event = game.ReplicatedStorage.SittingInABoat
local function func(character, seat)
local player = Players:GetPlayerFromCharacter(character)
end
event.Event:Connect()

TorpedoScript tells the server to destroy a weld, and make the torpedo start moving.

local seat = script.Parent.Parent.VehicleSeat
local force = script.Parent.LinearVelocity
local function fire()
print(“fired”)
script.Parent.Parent.Connector.ConnectCons:Destroy()
force.LineVelocity = 5
end
game.ReplicatedStorage.TorpedoEvent.OnServerEvent:Connect(fire)

There’s a bunch of different ways to do this. One way I see with your current setup:

  • In the local script, whenever E is pressed, fire the remote event for releasing a torpedo
  • On all ships, listen to whenever the torpedo event is triggered, and call a function
  • Have some validation in that function which checks to see whether the person driving the ship is the same as the person that called the remote event.

This is easily achieved, because whenever a client triggers a remote event, the player object belonging to that user is automatically passed through as a parameter.

-- client script
function inputBegan(input)
     if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
              -- fire the torpedo event
              TorpedoEvent:Fire()
        end
     end
end

UIS.InputBegan:Connect(inputBegan)

-- torpedo script
function fire(player)
    if Seat.Occupant.Parent.Name == player.Name then
         -- release the torpedo
    end
end


TorpedoEvent.OnServerEvent:Connect(fire)

hope this helps

1 Like

This helped immensely, thank you very much!

1 Like

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