Shotgun not working as wanted

Hi! I’ve made a shotgun, and when I shoot a zombie with my gun, the zombie freezes mid-air, then floats and falls apart, all while the zombie is still alive. The HumanoidRootPart of the torso becomes invisible but still follows the player and attacks me. However, I want the zombie to keep walking and only have a bit of damage when I shoot it.

Additionally, I tried to create an ammo GUI above the gun to display the ammo count, but the GUI doesn’t appear. Does anyone know how to fix this issue? By the way, here is the script:
local tool = script.Parent
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local bulletPrefab = replicatedStorage:WaitForChild(“BulletPrefab”)

local bulletsFolder = game.Workspace:WaitForChild(“Bullets”)
local zombieFolder = game.Workspace:WaitForChild(“Zombies”) – Assuming zombies are in a folder named “Zombies”

local MAX_BULLETS = 3 – Maximum bullets in the gun
local RELOAD_TIME = 3 – Time in seconds to reload
local SHOOT_COOLDOWN = 0.5 – Cooldown between shots in seconds

local bulletCount = MAX_BULLETS
local isReloading = false
local canShoot = true

– Create and update the bullet counter UI
local bulletCounter = Instance.new(“TextLabel”)
bulletCounter.Parent = tool
bulletCounter.Text = bulletCount … “/” … MAX_BULLETS
bulletCounter.Size = UDim2.new(0, 100, 0, 30)
bulletCounter.Position = UDim2.new(0, 0, 1, 10)
bulletCounter.BackgroundTransparency = 1
bulletCounter.TextColor3 = Color3.new(1, 1, 1)
bulletCounter.TextStrokeColor3 = Color3.new(0, 0, 0)
bulletCounter.TextStrokeTransparency = 0.5
bulletCounter.Font = Enum.Font.SourceSansBold
bulletCounter.TextSize = 20

local function updateBulletCounter()
bulletCounter.Text = bulletCount … “/” … MAX_BULLETS
end

local function onBulletHit(hit, bullet)
local character = game.Players:GetPlayerFromCharacter(hit.Parent)
local targetHumanoid = hit.Parent:FindFirstChild(“Humanoid”)

if character and targetHumanoid then
    targetHumanoid:TakeDamage(10)  -- Damage the humanoid
elseif hit.Parent:IsDescendantOf(zombieFolder) then
    -- Assuming zombies are in a folder named "Zombies"
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(10)  -- Damage the humanoid if it exists
    end
    -- Anchoring the zombie to prevent it from floating
    local zombieModel = hit.Parent
    for _, part in pairs(zombieModel:GetChildren()) do
        if part:IsA("BasePart") then
            part.Anchored = true
        end
    end
end

-- Create an explosion effect when the bullet hits something
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 5
explosion.BlastPressure = 1000
explosion.Position = bullet.Position
explosion.Parent = game.Workspace

bullet:Destroy()

end

local function createBullet(character)
if bulletCount <= 0 or isReloading or not canShoot then
return – No bullets, reloading, or on cooldown
end

bulletCount = bulletCount - 1
updateBulletCounter()
canShoot = false

local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")

local bulletSpeed = 100

-- Calculate the initial position of the bullet
local bulletPosition = humanoidRootPart.Position + humanoidRootPart.CFrame.lookVector * 2  -- Offset bullet position forward

-- Calculate the right vector based on the head's look vector
local headLookVector = (head.CFrame * CFrame.new(0, 0, -1)).lookVector
local bulletRightVector = headLookVector:Cross(Vector3.new(0, 1, 0)).unit

-- Adjust the bullet position with offsets
local bulletPositionOffset = headLookVector * 3 + bulletRightVector * 1 + Vector3.new(0, -0.1, 0)  -- Adjusted down and right offset
local bulletPositionAdjusted = bulletPosition + bulletPositionOffset

local bullet = bulletPrefab:Clone()
bullet.Position = bulletPositionAdjusted

-- Create BodyVelocity to move the bullet when unanchored
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = humanoidRootPart.CFrame.lookVector * bulletSpeed
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Parent = bullet

bullet.Parent = bulletsFolder

bullet.Touched:Connect(function(hit)
    onBulletHit(hit, bullet)
end)

wait(SHOOT_COOLDOWN)
canShoot = true

end

local function reloadGun()
isReloading = true
wait(RELOAD_TIME)
bulletCount = MAX_BULLETS
updateBulletCounter()
isReloading = false
end

tool.Activated:Connect(function()
if bulletCount <= 0 then
reloadGun()
elseif not isReloading then
local character = game.Players.LocalPlayer.Character
if character then
createBullet(character)
end
end
end)

– Ensure the tool variable is properly defined
if not tool or not tool:IsA(“Tool”) then
print(“Tool not found or incorrect type:”, tool)
else
– Parent the tool to the character’s backpack
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
tool.Parent = player.Backpack
end

– Initialize the bullet counter
updateBulletCounter()

2 Likes

is anyone gonna help because i don’t know how to figure this out

1 Like

You can try adding a boolean to the zombie where it tracks whether it should continue tracking players or not. For the ammo, did you make sure all enabled/visible properties are set to true? Try using print statements to see if certain events/functions are firing.

2 Likes

i did that yeah but it doesn’t have anything to do with the zombie but with the gun script itself

1 Like

What do you mean? The GUI? You mentioned the zombie kept tracking which I suggest adding a boolean. Also assuming by you saying “I did,” which I think you’re referring to the print statement, I noticed there aren’t any enabled or visible switching to true in your code.

2 Likes

ok that’s true idk what this bug is tho it is supposed to damage the zombie instead of tearing it apart

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