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:
- If you have an
Instance
created during run-time and need to refer to it.
- 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