My sound isn't playing?

So I’m making a script in whic when the player sits in a specified vehicle seat, the following code executes. Here is my script.

local Occupant = script.Parent.Occupant -- Humanoid or nil

if Occupant then --If there is a Humanoid Detected in the Seat
	print"A Player is in the Seat"
	-- After there is a player in the seat this code runs
	--Client
	local Event = game.ReplicatedStorage:WaitForChild("SoundHorn")
	local UIS = game:GetService("UserInputService")

	UIS.InputBegan:Connect(function(Key, GameDetect)
		if not GameDetect then return end --This will detect any non-ingame Inputs

		if Key.KeyCode == Enum.KeyCode.H then
			Event:FireServer()
		end
	end)

	--Server
	local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
	local horns = script.Parent.Parent:WaitForChild("HORNS")

	local fronthorn = horns:WaitForChild("FRONT")
	local fronthornstart = fronthorn:WaitForChild("279_HORN_START")
	local fronthornloop = fronthorn:WaitForChild("279_HORN_LOOP")
	local fronthornend = fronthorn:WaitForChild("279_HORN_END")

	Event.OnServerEvent:Connect(function()
		if UIS:IsKeyDown(Enum.KeyCode.H) then
			fronthornstart:Play()
			wait()
			fronthornstart:Stop()
			fronthornloop:Play()
		else
			fronthornloop:Stop()
			fronthornend:Play()
			wait()
			fronthornend:Stop()
		end

	end)


end



If it wasn’t obvious enough the first couple of lines chekcs if theres someone sitting in said seat, if theres someone sitting there then they can take full control of whatever the vehicleseat is controlling (in this scenario its a train). However when I attempt to sound the horn by pressing and holding H nothing happens.

Im starting to assume it has something to do with this select few lines

--Server
	local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
	local horns = script.Parent.Parent:WaitForChild("HORNS")

	local fronthorn = horns:WaitForChild("FRONT")
	local fronthornstart = fronthorn:WaitForChild("279_HORN_START")
	local fronthornloop = fronthorn:WaitForChild("279_HORN_LOOP")
	local fronthornend = fronthorn:WaitForChild("279_HORN_END")

	Event.OnServerEvent:Connect(function()
		if UIS:IsKeyDown(Enum.KeyCode.H) then
			fronthornstart:Play()
			wait()
			fronthornstart:Stop()
			fronthornloop:Play()
		else
			fronthornloop:Stop()
			fronthornend:Play()
			wait()
			fronthornend:Stop()
		end

	end)


end

So whats going on? What did I screw up? Thanks

I’m not the best scripter, but this is what I got:

local Occupant = script.Parent.Occupant -- Humanoid or nil

if Occupant then -- If there is a Humanoid detected in the Seat
    print("A Player is in the Seat")

    -- Client
    local Event = game.ReplicatedStorage:WaitForChild("SoundHorn")
    local UIS = game:GetService("UserInputService")

    UIS.InputBegan:Connect(function(input, gameProcessed)
        if gameProcessed then return end -- Ignore input if it's already processed by Roblox's UI

        if input.KeyCode == Enum.KeyCode.H then
            Event:FireServer() -- Fire a remote event to the server to sound the horn
        end
    end)

    -- Server
    local Event = game.ReplicatedStorage:WaitForChild("SoundHorn") -- Same remote event used by client
    local horns = script.Parent.Parent:WaitForChild("HORNS")
    local fronthorn = horns:WaitForChild("FRONT")
    local fronthornstart = fronthorn:WaitForChild("279_HORN_START")
    local fronthornloop = fronthorn:WaitForChild("279_HORN_LOOP")
    local fronthornend = fronthorn:WaitForChild("279_HORN_END")

    Event.OnServerEvent:Connect(function(player)
        fronthornstart:Play()
        wait()
        fronthornstart:Stop()
        fronthornloop:Play()
        wait(2) -- Adjust this wait time as needed
        fronthornloop:Stop()
        fronthornend:Play()
        wait()
        fronthornend:Stop()
    end)
end

Unfortunately this did not work. The sound didnt play at all. Not sure whats causing it though. :confused:
You did your best though and I appreciate that

First of all, output logs. Have you checked them? You should post them here and also read them yourself because a lot of these issues could have been solved by simply debugging the output for issues.

Now let’s start first with the client script and what issues it has.

local Occupant = script.Parent.Occupant -- Humanoid or nil

if Occupant then --If there is a Humanoid Detected in the Seat
	print"A Player is in the Seat"
	-- After there is a player in the seat this code runs
	--Client
	local Event = game.ReplicatedStorage:WaitForChild("SoundHorn")
	local UIS = game:GetService("UserInputService")

	UIS.InputBegan:Connect(function(Key, GameDetect)
		if not GameDetect then return end --This will detect any non-ingame Inputs

		if Key.KeyCode == Enum.KeyCode.H then
			Event:FireServer()
		end
	end)

Firstly, you shouldn’t have this script inside the seat. Instead you should aim to have character scripts in say, a GUI that is given when you enter the seat.

You should also preferably close off print statements like so: print("A Player is in the Seat")

You also forget to close the if Occupant then part of your code at the end, like so:

		if Key.KeyCode == Enum.KeyCode.H then
			Event:FireServer()
		end
	end)
end

Secondly, you are using UserInputService which is deprecated and arguably archaic. Using deprecated features is bad practice, so you should try to use ContextActionService instead. ContextActionService | Documentation - Roblox Creator Hub

Thirdly, you have some unnecessary WaitForChild usage. You do not need to wait for any Instance in the ReplicatedStorage unless they are created during run-time. In general, you should only use ReplicatedStorage in two cases:

  1. If you have an Instance created during run-time and need to refer to it.
  2. You are refering to objects in the Workspace and you have StreamingEnabled set to true.

This video explains this topic in further detail: https://www.youtube.com/watch?v=7tzy1DuPcBQ

Fourth, presuming that you want this horn to active when you hold the H button, you only check for if the InputHasBegan via UIS, instead you need to also check for the InputHasEnded for the desired effect.

You also have if not GameDetect then return end at the start of your code, which needs to be at the end of the InputBegan or InputEnd functions.

Lastly, you should also have a boolean to check if you are holding the input or not. You need this so you can pass the information to the server in an easy manner.

Also add waits to both the Input events and also, preferably use task.wait() as it is more effective than normal wait().

The corrected code should look something like this:

	local Holding = false

	UIS.InputBegan:Connect(function(Key, GameDetect)
		if Key.KeyCode == Enum.KeyCode.H then
			if Holding == false then
				Holding = true
				Event:FireServer(Holding)
				print("Event fired, input start")
				task.wait()
			end
		end
		
		if not GameDetect then return end --This will detect any non-ingame Inputs
	end)
	
	UIS.InputEnded:Connect(function(Key, GameDetect)
		if Key.KeyCode == Enum.KeyCode.H then
			if Holding == true then
				Holding = false
				Event:FireServer(Holding)
				print("Event fired, input end")
				task.wait()
			end
		end

		if not GameDetect then return end --This will detect any non-ingame Inputs
	end)

So yes, the client script is also the issue. Not purely the server script, which I will cover next.

	local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
	local horns = script.Parent.Parent:WaitForChild("HORNS")

	local fronthorn = horns:WaitForChild("FRONT")
	local fronthornstart = fronthorn:WaitForChild("279_HORN_START")
	local fronthornloop = fronthorn:WaitForChild("279_HORN_LOOP")
	local fronthornend = fronthorn:WaitForChild("279_HORN_END")

	Event.OnServerEvent:Connect(function()
		if UIS:IsKeyDown(Enum.KeyCode.H) then
			fronthornstart:Play()
			wait()
			fronthornstart:Stop()
			fronthornloop:Play()
		else
			fronthornloop:Stop()
			fronthornend:Play()
			wait()
			fronthornend:Stop()
		end
	end)
end

Firstly, you refer to local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") while you refered to local Event = game.ReplicatedStorage:WaitForChild("SoundHorn") in the client script. You are not referring to the same events, this breaks the script.

Secondly, you try to use UserInputService without refering to it in any shape or form. UserInputService would not work in the server script to begin with unless you specified the player via the RemoteEvent function. This is also unneeded, because the client script already checks for the input being held, if you were to add the boolean check discussed earlier.

And thirdly, again, you overuse WaitForChild which is unneeded as stated before.

The corrected server code would look something like this:

	Event.OnServerEvent:Connect(function(plr,holding)
		if holding == true then
			fronthornstart:Play()
			task.wait()
			fronthornstart:Stop()
			fronthornloop:Play()
		else
			fronthornloop:Stop()
			fronthornend:Play()
			task.wait()
			fronthornend:Stop()
		end

	end)

Over all, the code has a lot of issues. Lot of which could have been solved on your own initiative by studying the output which you seemingly did not take into account as it was not posted in this post. I heavily recommend you to use your output a lot more, and especially if posting on the DevForum, posting it so your code can be debugged easier. Some people here outright demand output logs to be given for their own ease, so from now on always aim to do so.

I also highly recommend you research the given resources and read into ROBLOX documentation over all. Lot of issues can be solved by researching issues there. Platform Overview | Documentation - Roblox Creator Hub

1 Like

Oh yeah ive checked the output several times. Nothing really popped up so I couldn’t tell what I had dont wrong. So thats why I posed here to clarify.

And also thank

Yeah I’m deeply sorry, scripting isnt my major skill so thats why the script looks pretty ugly

with ONE tiny typo, it doesnt work.

you see print"A Player is in the Seat" ?
make sure the text is closed with parenthesis. not sure if thats the cause tho…

ex; print("A Player is in the Seat")

Whoops, should’ve seen that. I fixed it just now and I will test the game to make sure if everything is running as expected

This does not break the code, I outlined all the reaosons above. In my post.

Sorry for the rather foolish question but would this be best in a local or server script? Im going to assume local since its in a Screen Gui and such

1 Like

No question is foolish.

You should insert it via a local script into the PlayerGUI, the GUI should have the script inside of it.

Once the player leaves the seat, you can :Destroy() the instance and set it to nil.

An example would be this for the script in the seat:


local plr = script.Parent.Occupant
local rs = game:GetService("ReplicatedStorage")

if plr then
      local UI = rs.TrainUI:Clone() -- GUI will contain the control for the horn
      UI.Parent = plr.PlayerGui
else
      UI:Destroy()
      UI = nil
end

I wrote this on my phone, so I apologize if there are any mistakes. But that should be the jist of it.

1 Like

Did you try using Script Analysis besides just Output? What about the debugging tools?

It seems like you’re having a Logic Error, so using the debugging tools should help. Perhaps it isn’t this part that’s causing the problem.

fronthornstart:Play()
			wait()
			fronthornstart:Stop()
			fronthornloop:Play()

Maybe it could be the if statements or that Event

*Edit: I think @anon39021680 mentioned the fix for this.

2 Likes

Try this:

-- Check if there is an occupant in the seat
local Occupant = script.Parent.Occupant -- Humanoid or nil

if Occupant then -- If there is a Humanoid detected in the Seat
    print("A Player is in the Seat")

    -- Client-side code
    local Event = game.ReplicatedStorage:WaitForChild("SoundHorn")
    local UIS = game:GetService("UserInputService")

    -- Connect a function to handle input events
    UIS.InputBegan:Connect(function(input, gameProcessed)
        if gameProcessed then return end -- Ignore input if it's already processed by Roblox's UI

        -- Check if the "H" key is pressed
        if input.KeyCode == Enum.KeyCode.H then
            Event:FireServer() -- Fire a remote event to the server to sound the horn
        end
    end)

    -- Server-side code
    local Event = game.ReplicatedStorage:WaitForChild("SoundHorn") -- Same remote event used by client
    local horns = script.Parent.Parent:WaitForChild("HORNS")
    local fronthorn = horns:WaitForChild("FRONT")
    local fronthornstart = fronthorn:WaitForChild("279_HORN_START")
    local fronthornloop = fronthorn:WaitForChild("279_HORN_LOOP")
    local fronthornend = fronthorn:WaitForChild("279_HORN_END")

    -- Handle the remote event when fired from the client
    Event.OnServerEvent:Connect(function(player)
        -- Play the horn sounds in sequence
        fronthornstart:Play()
        wait()
        fronthornstart:Stop()
        fronthornloop:Play()
        wait(2) -- Adjust this wait time as needed
        fronthornloop:Stop()
        fronthornend:Play()
        wait()
        fronthornend:Stop()
    end)
end

In this code:

  1. The script checks whether there’s an occupant in the seat. If an occupant is detected, it proceeds with the logic to handle client-side and server-side actions related to sounding a horn.
  2. On the client side, it waits for the “SoundHorn” remote event to be available in the ReplicatedStorage. It also listens for input events and triggers the remote event when the “H” key is pressed.
  3. On the server side, it waits for the same “SoundHorn” remote event. When the event is fired from the client, it plays a sequence of horn sounds (start, loop, and end) by accessing the respective sound objects.
  4. Both client-side and server-side code segments share the same remote event to synchronize the action of sounding the horn.
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.