Shotgun script doesn't work

“I have a shotgun script, but it doesn’t work as intended. I tried to make a bullet that spawns in a folder in Workspace when I click with the gun equipped. However, if I do that, the bullet gets anchored and doesn’t damage players. If I unanchor it, it just rolls, whereas I want the bullet to fly straight.”

4 Likes

Can we see the script by any chance?

3 Likes

yes i forgot sorry lol i was thinking about it but still forgot somehow

1 Like

local tool = script.Parent
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local bulletPrefab = replicatedStorage:WaitForChild(“BulletPrefab”)

local bulletsFolder = game.Workspace:WaitForChild(“Bullets”)

local function createBullet(character)
local humanoidRootPart = character:WaitForChild(“HumanoidRootPart”)

local bulletSpeed = 100

local bulletPosition = humanoidRootPart.Position + humanoidRootPart.CFrame.lookVector * 2 -- Offset bullet position forward
print("Bullet Position:", bulletPosition)

local bullet = bulletPrefab:Clone()
bullet.Position = bulletPosition
bullet.Velocity = humanoidRootPart.CFrame.lookVector * bulletSpeed -- Shoot in character's forward direction
bullet.Anchored = true -- Prevent the bullet from falling due to gravity
bullet.Parent = bulletsFolder

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

delay(5, function()
    if bullet and bullet.Parent then
        bullet:Destroy()
    end
end)

end

local function onBulletHit(hit, bullet)
bullet:Destroy()

local character = game.Players:GetPlayerFromCharacter(hit.Parent)
if character then
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(10)
    end
end

end

tool.Activated:Connect(function()
local character = game.Players.LocalPlayer.Character
if character then
createBullet(character)
end
end

– Make sure the tool variable is properly defined
if not tool or not tool:IsA(“Tool”) then
print(“Tool not found or incorrect type:”, tool)
end

1 Like

Do you get any errors while running the code in the console? Would help alot as the script looks fine to me.

i don’t have any errors in my output but when i use the script it just anchors the bullet and i can’t shoot it prob because in the script the bullet gets anchored i forgot you cant do velocity when the bullet is anchored but i tried this script unanchored and it just shoots out my torso and doesn’t do any damage and the bullet doesn’t go straight the bullet just falls straight down with a little bit velocity but i want it to go straight right where i shoot it

1 Like

Velocity doesn’t move an anchored part.
You can add a body velocity inside of the bullet and set BodyVelocity.MaxForce to Vector3.new(math.huge, math.huge, math.huge) which is equivalent to Vector3.one * math.huge. It won’t work with anchored parts but when you unanchor it it should work without falling.

aight imma try it hope it works

18:36:30.089 Players.Playfullgamer_YT.Backpack.Shotgun.GunScript:29: attempt to call a nil value - Client - GunScript:29
18:36:30.089 Stack Begin - Studio
18:36:30.090 Script ‘Players.Playfullgamer_YT.Backpack.Shotgun.GunScript’, Line 29 - Studio - GunScript:29
18:36:30.090 Stack End - Studio

– Shotgun Script: This script aims to create bullets that spawn in a Workspace folder when the gun is equipped and fired. The goal is for the bullet to fly straight and damage players upon impact.

local tool = script.Parent
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local bulletPrefab = replicatedStorage:WaitForChild(“BulletPrefab”)

local bulletsFolder = game.Workspace:WaitForChild(“Bullets”)

local function createBullet(character)
local humanoidRootPart = character:WaitForChild(“HumanoidRootPart”)

local bulletSpeed = 100

local bulletPosition = humanoidRootPart.Position + humanoidRootPart.CFrame.lookVector * 2 -- Offset bullet position forward
print("Bullet Position:", bulletPosition)

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

-- 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)

delay(5, function()
    if bullet and bullet.Parent then
        bullet:Destroy()
    end
end)

end

local function onBulletHit(hit, bullet)
bullet:Destroy()

local character = game.Players:GetPlayerFromCharacter(hit.Parent)
if character then
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(10)
    end
end

end

tool.Activated:Connect(function()
local character = game.Players.LocalPlayer.Character
if character then
createBullet(character)
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)
end

it did the same as what happens when i unanchored the bullet

What lines inside the script are these?

it works but it shoots out my torso and i want ti to shoot out my gun tho wdym lines inside the script?

It shoots it from the torso because of this line here.

oooh ty what should i do instead of that line?

i changed the code and it worked thank you:D

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