- What do you want to achieve? Keep it simple and clear!
I want to fix my gun sound script. I’ve made it so my gun plays different gunshots when inside the building.
- What is the issue? Include screenshots / videos if possible!
The gun plays the same sound, yet when I check the value for it, it changes.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried to fix it, but nothing works.
The code is designed so when the Ray hits a part on top of the player, it changes the gun sound.
Here’s the code:
--LocalScript in StarterCharacterScripts
function checkRay(x)
local Muzzle = WeaponInHand.Handle.Muzzle
local head = x:FindFirstChild("Head")
local origin = head.Position
local direction = Vector3.new(origin.X, 12, origin.Z)
local raycastResult = workspace:Raycast(origin, direction)
if raycastResult and raycastResult.Instance:IsA("Part") then
local newSound = Muzzle.Fire2:Clone()
newSound.PlaybackSpeed = newSound.PlaybackSpeed + math.random(-20,20) / 1000
newSound.Parent = Muzzle
newSound.Name = "Firing"
newSound:Play()
newSound.PlayOnRemove = true
newSound:Destroy()
else
local newSound = Muzzle.Fire:Clone()
newSound.PlaybackSpeed = newSound.PlaybackSpeed + math.random(-20,20) / 1000
newSound.Parent = Muzzle
newSound.Name = "Firing"
newSound:Play()
newSound.PlayOnRemove = true
newSound:Destroy()
end
end
--checkRay(char)
I would really appreciate the help!