I have a gun system and I’m trying to add Bullet spread. The gun system works by cloning the bullet to a part or now a random part out of 5
For some reason the bullets are coming out of where the gun was laying on the ground.
Everything is welded to the handle nothing is anchored and I tried both CFrame and Position with neither working
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
This is the cloning script
local function CreateBullet(mouseLookVector)
local NewBullet = Bullet:Clone()
if BPV.Value == 1 then NewBullet.Position = BulletPoint.Position
end
if BPV.Value == 2 then NewBullet.Position = BulPoint2.Position
end
if BPV.Value == 3 then NewBullet.Position = BulPoint3.Position
end
if BPV.Value == 4 then NewBullet.Position = BulPoint4.Position
end
if BPV.Value == 5 then NewBullet.Position = BulPoint5.Position
end
local direction
if GunInfo.ShotgunAspect then
local x = math.random(-GunInfo.Spread*100, GunInfo.Spread*100)/100
local y = math.random(-GunInfo.Spread*100, GunInfo.Spread*100)/100
else
direction = mouseLookVector
end
local OriginalDamage = GunInfo.Damage
The parts where the bullets come from is the part that is in front of the crosshair. The gun is not welded to the hand its just a tool with animations (Unless the tool welds to the players hand when equipped on its own)
So the bullets are parts themselves and this is the bullet script
local Configs = script.Parent:WaitForChild("Configs")
local BulletHole1 = game.ReplicatedStorage.BulletHole1
local HitSound = script.Parent:WaitForChild("ZombieHItSound")
script.Parent.Touched:Connect(function(hit)
local CheckifZombie
if hit.Parent then
CheckifZombie = hit.Parent:FindFirstChild("Zombie")
end
if CheckifZombie and string.match(hit.Name, "Bullet") == nil then
if hit.Name == "Head" then
if Configs.Headshot.Value == true then
CheckifZombie:TakeDamage(Configs.Damage.Value * Configs.HeadshotMultiplier.Value)
HitSound:Play()
else
CheckifZombie:TakeDamage(Configs.Damage.Value)
HitSound:Play()
end
else
CheckifZombie:TakeDamage(Configs.Damage.Value)
end
script.Parent:Destroy()
else
local isHuman
if hit.Parent then
isHuman = hit.Parent:FindFirstChild("Humanoid")
end
if hit.CanCollide == true and isHuman == nil then
script.Parent:Destroy()
end
end
end)
It seems like the issue may be with the direction of the bullets being fired. In the case of the shotgun aspect, you are correctly using random values to create a spread, but in the else case, you are not setting a direction for the bullets. You should add a direction variable in the else case as well, similar to how you did it in the shotgun aspect.
Additionally, you may want to consider using a switch statement instead of a series of if statements to determine the position of the bullet. This can help make your code more organized and easier to read.
To use a switch statement, you can use the following syntax:
local value = 5
switch value do
case 1:
-- code to run when value is 1
case 2:
-- code to run when value is 2
case 3:
-- code to run when value is 3
default:
-- code to run when value is not 1, 2, or 3
end
In your case, you can use a switch statement like this.
local function CreateBullet(mouseLookVector)
local NewBullet = Bullet:Clone()
local direction
switch BPV.Value do
case 1:
NewBullet.Position = BulletPoint.Position
case 2:
NewBullet.Position = BulPoint2.Position
case 3:
NewBullet.Position = BulPoint3.Position
case 4:
NewBullet.Position = BulPoint4.Position
case 5:
NewBullet.Position = BulPoint5.Position
default:
-- code to run if BPV.Value is not 1, 2, 3, 4, or 5
end
if GunInfo.ShotgunAspect then
local x = math.random(-GunInfo.Spread*100, GunInfo.Spread*100)/100
local y = math.random(-GunInfo.Spread*100, GunInfo.Spread*100)/100
else
direction = mouseLookVector
end
local OriginalDamage = GunInfo.Damage
end
This switch statement will set the position of the bullet based on the value of BPV.Value. If BPV.Value is not 1, 2, 3, 4, or 5, the default case will run. You can add additional cases for different values if needed.
You are correct, switch statements do not exist in Lua or Luau. Instead, you can use a combination of if-else statements or a table of functions to achieve a similar effect.
To use if-else statements, you can use the following syntax:
local value = 5
if value == 1 then
-- code to run when value is 1
elseif value == 2 then
-- code to run when value is 2
elseif value == 3 then
-- code to run when value is 3
else
-- code to run when value is not 1, 2, or 3
end
local Damage = GunInfo.Damage
if GunInfo.CriticalMultiplier then
if math.random(1, GunInfo.CriticalChance) == 1 then
Damage = Damage * GunInfo.CriticalMultiplier
end
end
local newDamage = Damage
if GunInfo.DamageAmmount then
Damage = GunInfo.DamageAmmount
if newDamage then
Damage = newDamage
end
end
NewBullet.Name = "Bullet"
NewBullet.Parent = workspace
local NewRay = Ray.new(NewBullet.Position, direction)
local NewHit, NewHitPos, NewHitNormal = workspace:FindPartOnRayWithIgnoreList(NewRay, {LocalPlayer.Character, PlayerGui, Gun})
local Distance = (NewHitPos - NewBullet.Position).magnitude
NewBullet.Touched:connect(function(hit)
if not hit:IsDescendantOf(LocalPlayer.Character) and not hit:IsDescendantOf(Gun) then
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid.Health == humanoid.MaxHealth then
humanoid:TakeDamage(Damage)
end
end
if GunInfo.Killstreak then
if GunInfo.Killstreak == "Headshot" then
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid.Health == humanoid.MaxHealth then
humanoid:TakeDamage(OriginalDamage*3)
end
end
end
end
if GunInfo.DamageAmm
So after taking a long break I realized why am I using other parts when I can just add Vector 3 and Math.Randoms to the bullets spawn position. I cant believe I didn’t think about this right away… haha.
Thanks for all the help everyone! sorry for my smooth brain