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

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

Asked by 6 years ago

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.

0
You are asking people to write the whole script for you. That is against the rules. User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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
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. Athedin 4 — 6y
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' User#20388 0 — 6y
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. Athedin 4 — 6y
0
I'm gonna edit my script, please accept my answer! User#20388 0 — 6y
View all comments (18 more)
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. Athedin 4 — 6y
0
No, there should be a button named 'accept answer' under my answer User#20388 0 — 6y
0
Can't seem to find it, even with Ctrl+F and typing "Accept" it doesn't show up except in the replies. Athedin 4 — 6y
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. Athedin 4 — 6y
0
It's next to the 'report' flag, called 'Accept answer' under these comments User#20388 0 — 6y
0
I only have a report button, weird. Athedin 4 — 6y
0
Fixed it User#20388 0 — 6y
0
That's very weird, like, impossible weird, are you sure? User#20388 0 — 6y
0
Still acts like the first script, and I'm pretty certain I made an exact copy =/ https://pastebin.com/r8qdywyC Athedin 4 — 6y
0
Can you copy the error again? User#20388 0 — 6y
0
Btw, I'll edit my script so your sound thing should work User#20388 0 — 6y
0
What do you mean with copy the error??? Athedin 4 — 6y
0
Oh and it works now, thanks a lot!! Athedin 4 — 6y
0
Np, can you accept this answer though xD User#20388 0 — 6y
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 :( Athedin 4 — 6y
0
oh wonderful, script works in studio but not in-game. gotta love roblox sometimes Athedin 4 — 6y
0
Lol User#20388 0 — 6y
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 Athedin 4 — 6y
Ad

Answer this question