What do I want to achieve? Keep it simple and clear!
Make this car horn system audible to other players, not just the driver only.
What is the issue? Include screenshots / videos if possible!
The horn only plays for the driver, which makes it basically useless.
Which solutions did I try so far?
Changing the LocalScript to a normal script (I’m still new to scripting, don’t judge me lol)
More Details:
Arrangement in workspace:
Script “Control”
if script.Parent.Parent:IsA("VehicleSeat") then
script.Parent.Parent.ChildAdded:connect(function(child)
if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
local p=game.Players:GetPlayerFromCharacter(child.Part1.Parent)
local g=script.G:Clone()
g.Parent=p.PlayerGui
g:WaitForChild("src")
g.src.Value=script.Parent
g.Horn.Disabled=false
end
end)
end
Disabled LocalScript “Horn”
local mouse=game.Players.LocalPlayer:GetMouse()
script.Parent:WaitForChild("src")
src=script.Parent.src.Value
mouse.KeyDown:connect(function(key)
if key=="h" then
src:Play()
end
end)
mouse.KeyUp:connect(function(key)
if key=="h" then
src:Stop()
end
end)
src.Parent.ChildRemoved:connect(function(child)
if child.Name=="SeatWeld" then
src:Stop()
script.Parent:Destroy()
end
end)
Thank you! If you need more informations, please don’t hesitate to ask.
You’ll need to use a RemoteEvent for this, I believe. Sounds played by a client can only be heard by that client, while a client requesting the server to play a Sound through a RemoteEvent will cause the sound to be played on the server (with some latency, depending on how fast the client’s internet speed is.)
A RemoteEvent can be placed anywhere a LocalScript and a normal Script can access. In order to get the client to tell the server to play the Sound, have the code setup kind of like this:
Server:
-- ... Your variables...
local Remote_Event = Path.To.RemoteEvent
-- ... More variables...
Remote_Event.OnServerEvent:Connect(function(player, <your other args>)
-- ... Your code
end)
Client:
-- Your variables...
-- Your RemoteEvent(s)
local function Somehow_Detect_Vehicle_And_Get_Remote_Event()
-- Your code to detect whether your character sits on a vehicle. Make sure to get the RemoteEvent(s) too.
end
-- More code...
-- Your horn function. Just an example:
local function Horn(Signal)
if not (Signal.Keycode == Enum.Keycode.H) then return end
if Sitted then
if Vehicle_Remote_Event then
Vehicle_Remote_Event:FireServer(<your args>)
end
end
end
-- Connect your Horn function with UIS or something.
Don’t use my example, as I never tested it, and the code is made in a rush. The idea about RemoteEvents, though, is still there. For more information, check out these links from the DevHub:
local HornEvent = script.Parent
local HornSound = --Add the Horn
HornEvent.OnServerEvent:Connect(function(play)
if play = "Play"
HornSound:Play() --This will play the sound for everyone
elseif play = "Stop" then
HornSounch:Stop()
end
end)
Local Script:
local HornEvent = game.ServerScriptServer:WaitForChild("HornEvent")
mouse.KeyDown:connect(function(key)
if key=="h" then
local play = "Play"
HornEvent:FireServer(play)
end
end)
mouse.KeyUp:connect(function(key)
if key=="h" then
local play = "Stop"
HornEvent:FireServer(play)
end
end)
I know the solution because i had exact same issue… just the sound Horn (not everything within it) needs to be located in the workspace not within the model. I use a very simple server script and put a good roll off distance and it will be heard by everyone.
I also have a gui pop up when player sits and this controls the horn.
So, I realized that playing sounds in the local server or activating them in the local server won’t help. Locals script’s are used for player specific event’s, that only that certain player can see and interact with. While server script’s do event’s in the server. Which every player can see and interact with, The Solution is playing it in a server script which plays it to all the players(server script means a regular script).
So this means that the horn must be placed in workspace, the driver seat has a local script as a parent firing a RemoteEvent and this event plays the audio, right?
So, The solution is the Horn must be placed in the workspace. the driver seat should have a normal script instead of a local script(No need of remote event). To explain in detail, the local script only plays the sound for one player and the normal scripts plays for all.
Just Copy the script of local script and place it in a normal script, make the normal script disabled. also don’t forget to delete the local script.
I have the sound in workspace, then my driver seat has a server script that handles the gui and just plays the horn sound when the gui located within it is touched.
The server script in the gui should look like this:
local horn = workspace.*YourSoundHere*
script.Parent.MouseButton1Down:Connect(function()
horn:Play()
end)
There are a couple things wrong with this code. First, KeyDown and KeyUp are deprecated by UserInputService. Second, LocalScripts don’t work in workspace, they need to be placed either in StarterPlayerScripts or StarterGui.
local UserInputService = game:GetService("UserInputService")
local HornEvent = game.ServerScriptServer:WaitForChild("HornEvent")
UserInputService.InputBegan:Connect(function(key)
if key == Enum.KeyCode.H then
local play = "Play"
HornEvent:FireServer(play)
end
end)
UserInputService.InputEnded:Connect(function(key)
if key == Enum.KeyCode.H then
local play = "Stop"
HornEvent:FireServer(play)
end
end)
Thanks, but now I’m totally lost. What do I have to do with this piece of code you gave me? Is this supposed to be in a script as a parent of a RemoteEvent?