#Sound not playing. I have a script firing a remote event, that a local script picks up.

1 messages · Page 1 of 1 (latest)

pearl vale
#

I have a script firing a remote event, that a local script picks up.

The sound that I am playing is cloned into the humanoidrootpart when a player joins the game.

The server script is inside a knife. When a player holds down right click, then lets go this knife is cloned into the workspace. When the knife hits a player, the event should be fired. Here is the code

local playerthrownid = script.Parent:GetAttribute("PlayerThrownId")
local hasHit = false

script.Parent.Touched:Connect(function(hit)
    if hasHit then return end

    local character = hit:FindFirstAncestorOfClass("Model")
    if not character then return end

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    local player = game.Players:GetPlayerFromCharacter(character)
    if not player then return end

    local hitPlayerId = player.UserId

    if hitPlayerId == playerthrownid then return end
    hasHit = true
    humanoid:TakeDamage(35)
    local PlayerWhoThrew = game.Players:GetPlayerByUserId(playerthrownid)
    game.ReplicatedStorage.ThrownKnifeHitSound:FireClient(PlayerWhoThrew, hitPlayerId)
    print("FiredEvent")
    script.Parent.Parent:Destroy()
end)

This local script is under StarterCharacterScripts

local player = game.Players.LocalPlayer
local character = game.Workspace:FindFirstChild(player.Name)
local humanoidrootpart = character:FindFirstChild("HumanoidRootPart")
local hitSound = humanoidrootpart.Swinghit
local SwingHitEvent = game.ReplicatedStorage.ThrownKnifeHitSound

SwingHitEvent.OnClientEvent:Connect(function(player, id)
    print("SwingHitEvent fired")
    hitSound:Play()
end)
deft jay
#

Maybe the sounds plays before the character even loads?

shrewd ember
#

Server Script (inside the thrown knife)

local playerthrownid = script.Parent:GetAttribute("PlayerThrownId")
local hasHit = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local HitEvent = ReplicatedStorage:WaitForChild("ThrownKnifeHitSound")

script.Parent.Touched:Connect(function(hit)
if hasHit then return end

local character = hit:FindFirstAncestorOfClass("Model")
if not character then return end

local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end

local playerHit = Players:GetPlayerFromCharacter(character)
if not playerHit then return end

-- prevent self-hit
if playerHit.UserId == playerthrownid then return end

hasHit = true
humanoid:TakeDamage(35)

local playerWhoThrew = Players:GetPlayerByUserId(playerthrownid)
if playerWhoThrew then
    -- Only send hitPlayerId to the thrower's client
    HitEvent:FireClient(playerWhoThrew, playerHit.UserId)
end

print("FiredEvent to", playerthrownid, "about hit on", playerHit.UserId)

script.Parent:Destroy()

end)

#

Local Script (under StarterCharacterScripts)

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SwingHitEvent = ReplicatedStorage:WaitForChild("ThrownKnifeHitSound")

-- Function to connect event when the character is ready
local function setupCharacter(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local hitSound = humanoidRootPart:WaitForChild("Swinghit")

-- Listen for the RemoteEvent from server
SwingHitEvent.OnClientEvent:Connect(function(hitPlayerId)
    print("SwingHitEvent fired! You hit player with ID:", hitPlayerId)
    hitSound:Play()
end)

end

-- Handle both initial spawn and respawns
if player.Character then
setupCharacter(player.Character)
end

player.CharacterAdded:Connect(setupCharacter)

#

Key Fixes I made:
FireClient arguments are correct — the first param is the recipient, all others are just data.
No early access to character parts — I used CharacterAdded + WaitForChild to make sure the HumanoidRootPart and the Swinghit sound exist before trying to use them.
Respawn-safe — if the thrower dies and respawns, the listener still works.
Cleaned server logic — prevents self-hits, only fires once per throw, destroys the knife after.

#

@pearl vale

pearl vale
shrewd ember
# pearl vale Thank you, but for learning purposes what was wrong with my code?

Alright, so here’s why your script wasn’t doing what you expected:
1. FireClient mix-up
When you do

FireClient(player, arg1, arg2)

the first thing you pass in is who gets the event. Everything after that is just data.
In your local script, the first parameter in your function (player) wasn’t actually a Player — it was the hitPlayerId you sent from the server. And since you only sent one extra value, your second parameter (id) was just nil.

2.    Grabbing the character too soon

Your script grabbed the character and HumanoidRootPart right away when it ran. If the player hadn’t fully spawned yet, those were nil. And if the Swinghit sound was cloned in a bit later, your script would never find it.
3. No respawn handling
You only set humanoidrootpart and hitSound once. When the player dies, the character is replaced with a new one — your script doesn’t update, so it can’t play the sound after a respawn.
4. Possible timing race
If the knife hit happened right after being thrown, the event could fire before the sound existed in the player’s character — so nothing would play, even though the event technically worked.

Basically:
The parameters on the client didn’t match what you were sending.
The script tried to use stuff that didn’t exist yet.
It didn’t reconnect when the player respawned.

Once you fix those, the logic works fine.