So what I’m trying to achieve is detecting a UserInput from a user that is seated at my cannon, and so far I’m a bit lost on how to achieve this. I just want to detect if the user for example presses the “F” Key to fire the cannon.
1 Like
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Seated
https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction
4 Likes
So to detect the User’s input, all I have to do is just bind the input to an action when they are seated?
Use seated event to detect when they sit down, probably then have a function to handle them firing the cannon and have it bound to the keycode of F
So this script that detects when they are seated will be in StarterPlayerScripts right?
Heres an example script (local script in startercharacter), pressing f while sitting down prints “pew”
https://gyazo.com/d4dde69a7ea588b805c26ed05a668dc0
local cas = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
function fire(_,state)
if state == Enum.UserInputState.Begin then
print("pew")
end
end
humanoid.Seated:Connect(function(active,seat)
if active and seat then
cas:BindAction("FireCannon",fire,false,Enum.KeyCode.F)
else
cas:UnbindAction("FireCannon")
end
end)
3 Likes
Alright I got it thanks for the help. This is something I’ve been stuck on for a few days now :).