#Need help with the gun script

6 messages · Page 1 of 1 (latest)

upbeat kraken
#

so i made this gun script for my working gun but when i tested it it didnt work so can anyone help me find the problem
local gun = script.Parent
local bulletSpawnPoint = gun:WaitForChild("BulletSpawnPoint")
local bulletTemplate = game.ReplicatedStorage:WaitForChild("Bullet")
local mouse = game.Players.LocalPlayer:GetMouse()

local function shoot()
local bullet = bulletTemplate:Clone()
bullet.Parent = game.Workspace.Bullets

bullet.Position = bulletSpawnPoint.Position

local direction = (mouse.Hit.p - bulletSpawnPoint.Orientation).Unit

bullet.Velocity = direction * 150

bullet.Touched:Connect(function(hitPart)
     local hitCharacter = hitPart:FindFirstChild("Humanoid")

     if hitCharacter and hitCharacter.Health > = 1 then
           hitCharacter.Health -= math.random(10,25)

           if hitCharacter.Health < = 0 then
                 hitCharacter.Died:Fire()
                 hitPart.Parent:Destroy()
           end
           else
               print("Hit something that is not a player")
        end

        wait(2)
        bullet:Destroy()
    end)

end

gun.Activated:Connect(shoot)

upbeat kraken
#

a

compact vine
#

Yo try this

#
local bulletSpawnPoint = gun:WaitForChild("BulletSpawnPoint")
local bulletTemplate = game.ReplicatedStorage:WaitForChild("Bullet")
local mouse = game.Players.LocalPlayer:GetMouse()

local function shoot()
    local bullet = bulletTemplate:Clone()
    bullet.Parent = game.Workspace.Bullets

    bullet.Position = bulletSpawnPoint.Position

    local direction = (mouse.Hit.Position - bulletSpawnPoint.Position).Unit

    bullet.Velocity = direction * 150

    bullet.Touched:Connect(function(hitPart)
        local hitCharacter = hitPart.Parent:FindFirstChild("Humanoid")

        if hitCharacter and hitCharacter.Health >= 1 then
            hitCharacter.Health = hitCharacter.Health - math.random(10, 25)

            if hitCharacter.Health <= 0 then
                hitCharacter:TakeDamage(hitCharacter.Health)  -- Correct way to apply damage
                hitPart.Parent:Destroy()
            end
        else
            print("Hit something that is not a player")
        end

        wait(2)
        bullet:Destroy()
    end)
end

gun.Activated:Connect(shoot)
upbeat kraken
#

aight

#

@compact vine yoo ty