Frequently repeated sound stops playing after some time when not intended to

I want a laser sound effect to play each time this raycast hits something

Sound doesn’t play when intended. At first, it plays, but then stops, even though the raycast continues to hit a part. When I remove the part so that the raycast returns nil, and then move the part back so that the part gets his by the raycast, the sound plays as intended.

This is the Script:

local ServerStorage = game:GetService("ServerStorage")
local rayPart = ServerStorage:WaitForChild("rayPart")
local SFX = script.Parent.laserMusket

local rcParams = RaycastParams.new()
rcParams.FilterType = Enum.RaycastFilterType.Exclude
rcParams.FilterDescendantsInstances = {script.Parent}


while wait(1) do
	local startPosition = script.Parent
	local raycastResult = workspace:Raycast(script.Parent.Position, startPosition.CFrame.UpVector*20, rcParams)
	print(raycastResult)
	if raycastResult ~= nil then
		local bullet = rayPart:Clone()
		bullet.Anchored = true
		bullet.CanCollide = false
		bullet.Size = bullet.Size + bullet.CFrame.UpVector*raycastResult.Distance
		bullet.CFrame = script.Parent.CFrame + script.Parent.CFrame.UpVector*raycastResult.Distance/2
		bullet.Parent = workspace
		if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			raycastResult.Instance.Parent:FindFirstChild("Humanoid").Health -= 10
		end
		SFX:Play()
		wait()
		bullet:Destroy()
	end
end
1 Like

Try putting SFX:Play() before the if statement to find the humanoid. and also change “Health -= 10” to “:TakeDamage(10)”

Just put the SFX in the if raycastresult statement

This solution helped the sound play more consistenly, but there were still times where the beam would shoot and no sound would play. Do you know of a better way to play the sound?

I don’t get what you mean. How would I do this?

after the “if raycastResult ~= nil then” statement put the “SFX:Play()” and set the TimePosition to 0

If that doesn’t work, then make a cloned instance of “SFX” and parent it to the bullet, and when the bullet is parented to workspace, then play it.

Your 2nd suggestions worked when I enabled the PlayOnRemove property for the sound. Thanks for your help

1 Like

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