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)