Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so a keyboard button does a thing but ONLY if you sit in a certain seat?

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.

1 comment

0

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

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

0

Do you put that script inside the seat? And what exactly do you mean with "--> Local script!!". Sorry, I'm a beginner to Lua.

0

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'

0

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.

0

I'm gonna edit my script, please accept my answer!

0

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.

0

No, there should be a button named 'accept answer' under my answer

0

Can't seem to find it, even with Ctrl+F and typing "Accept" it doesn't show up except in the replies.

0

"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.

0

It's next to the 'report' flag, called 'Accept answer' under these comments

0

I only have a report button, weird.

0

Fixed it

0

That's very weird, like, impossible weird, are you sure?

0

Still acts like the first script, and I'm pretty certain I made an exact copy =/ https://pastebin.com/r8qdywyC

0

Can you copy the error again?

0

Btw, I'll edit my script so your sound thing should work

0

What do you mean with copy the error???

0

Oh and it works now, thanks a lot!!

0

Np, can you accept this answer though xD

0

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 :(

0

oh wonderful, script works in studio but not in-game. gotta love roblox sometimes

0

Lol

0

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.