Horn Script Doesn't Work

I am trying to make a script where if a player presses a text button, a sound (the horn) plays. The LocalScript is inside of the VehicleSeat. For some reason, the script doesn’t work. I tried changing the code up, but it didn’t work. I also made sure everything I defined (seatPart, part, etc) is correctly referencing the in-game item.

The LocalScript inside of the VehicleSeat. (The part is inside of the VehicleSeat’s parent (the car), and the sound is inside of the part.)

local seatPart = script.Parent
local part = seatPart.Parent:WaitForChild("Part")
local sound = part:WaitForChild("Horn")

local button = game.StarterGui.ScreenGui2.TextButton

if sound then
	button.MouseButton1Down:Connect(function()
		sound:Play()
	end)

	button.MouseButton1Up:Connect(function()
		sound:Stop()
	end)
end

Lmk what yall think.

1 Like

Many problems:

First local scripts will never run if they’re parented in workspace, instead. Parent this local script in startercharacterscripts/starterplayerscripts so it actually runs (or startergui if you wish but I recommend either the first two as its easier for organisation).

Second, you’re LITERALLY referencing the startergui which means it wouldn’t work anyway (because everything in the startergui service is CLONED to the player’s playergui, so you’re accessing the wrong version of it):

Thirdly, the “if sound” statement would kinda be useless since you are yielding until it loads for the client anyway. (Not really an issue but kinda weird)

Here’s a new script:

-- Parent this local script in startercharacterscripts
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Car = workspace:WaitForChild("Car") -- This may not be the actual name of your car so change it
local seatPart = Car:WaitForChild("seatPart") -- Again may not be the actual name of the seatpart aswell

local part = seatPart.Parent:WaitForChild("Part")
local sound = part:WaitForChild("Horn")

local PlayerGui = Player:WaitForChild("PlayerGui")
local ScreenGui2 = PlayerGui:WaitForChild("ScreenGui2")
local button = ScreenGui2:WaitForChild("TextButton")
local Debounce = false

seatPart:GetPropertyChangedSignal("Occupant"):Connect(function() -- This is to check if the player is sitting in the seat and to not run the event when they leave it
if SeatPart.Occupant = Character:WaitForChild("Humanoid") then
Debounce = true
else
Debounce = false
end)

button.MouseButton1Down:Connect(function()
if Debounce then
sound:Play()
end
end)

button.MouseButton1Up:Connect(function()
if Debounce then
sound:Stop()
end
end)

Also, this sound will only play for the player who presses the button and not be heard by the rest of the players in the server because it’s client-sided. But I’m not sure if that’s what you want, if it isn’t what you want then make sure to use remote events.

4 Likes