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