Basically what I want to do is improving a plane script. The plane script I use for my fighter planes allows them to do all maneuvers that a plane can do in real life, unlike other scripts. However, this script doesn't have any sounds for the weapons so what I want to do is when you sit in the pilot seat and press "C" (the gun button) you hear a cannon sound. I've figured out how to make something be triggered with a button, but I can't combine it with seats.
How do I make it so a keyboard button does a thing but ONLY if you sit in a certain seat?
1 answer
Well, simply use 'occupant' property of seats.
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer --> Local script!!
local Sound = game.SoundService.Sound
local Seat = workspace:WaitForChild('Seat') -- Your seat
Active = false
function onInputBegan(input,gameProcessed)
if input.KeyCode == Enum.KeyCode.C then
if seat.Occupant and Seat.Occupant.Parent.Name == Player.Name then
Active = true
end
end
end
function onInputEnded(input,gameProcessed)
if input.KeyCode == Enum.KeyCode.C then
if seat.Occupant and Seat.Occupant.Parent.Name == Player.Name then
Active = false
end
end
end
Seat.Changed:Connect(function(Prop)
if Prop == 'Occupant' then
local Occupant = Seat.Occupant
if Occupant then
-- Nothing
else
Active = false --> Player left the seat
end
end
end)
UserInputService.InputBegan:connect(onInputBegan)
UserInputService.InputEnded:connect(onInputEnded)
while wait() do
if Active == true then
-- Code
if Sound.IsPlaying == false then
Sound:Play()
end
else
if Sound.IsPlaying == true then
Sound:Stop()
end
end
end
22 comments
Do you put that script inside the seat? And what exactly do you mean with "--> Local script!!". Sorry, I'm a beginner to Lua.
You put the script inside a local area (like playergui, starterpack,..) and the code needs to be inside a localscript, Line 6 is a reference to the seat you're using, this script assumes your seat is called 'Seat' and is located inside workspace, that's why I added the comment 'Your seat'
The script works well! I did however notice a flaw in my question, it should be activated only when you hold down C, and not just when you press it. Sorry for the miss there.
I'm gonna edit my script, please accept my answer!
Do you mean accept as in upvote? If so I can't as this is my first help request and I therefor only have 0 in reputation, I need 25 to do it. Sorry if that was the case.
No, there should be a button named 'accept answer' under my answer
Can't seem to find it, even with Ctrl+F and typing "Accept" it doesn't show up except in the replies.
"Occupant" on line 25 is underscored with a blue line; "W001: Unknown global 'Occupant'". If I try the script anyway, it acts just like before; it plays even after you release the button.
It's next to the 'report' flag, called 'Accept answer' under these comments
I only have a report button, weird.
Fixed it
That's very weird, like, impossible weird, are you sure?
Still acts like the first script, and I'm pretty certain I made an exact copy =/ https://pastebin.com/r8qdywyC
Can you copy the error again?
Btw, I'll edit my script so your sound thing should work
What do you mean with copy the error???
Oh and it works now, thanks a lot!!
Np, can you accept this answer though xD
I really can't haha! I have no idea why it won't show https://gyazo.com/3400e5fba90aa88dbadcef9b7c0998d8 By the way, is it too much to ask for if I want a script that toggles sound instead? Needed for engine sound :(
oh wonderful, script works in studio but not in-game. gotta love roblox sometimes
Lol
ok im out. i wont torture u with more questions because i just feel bad, the plane will do without sounds then but whatever
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
You are asking people to write the whole script for you. That is against the rules.