Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my gun not reloading properly or damaging anything?

Asked by 4 years ago

There are a couple of things wrong with my gun. First, it does not reload when it reaches 0 ammunition. Second, it doesn't damage anything with a humanoid. Here are the scripts:

Client-Side:

--Global Variables
local shotgun = script.Parent.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local flare = shotgun:WaitForChild("Flare")
local handle = shotgun:WaitForChild("Handle")
local ContextActionService = game:GetService("ContextActionService")
local mouseDown = false
local equipped = false

--Folders
local animations = shotgun:WaitForChild("Animations")
local configurations = shotgun:WaitForChild("Configurations")
local remoteEvents = shotgun:WaitForChild("RemoteEvents")

--Animations
local reload = animations:WaitForChild("ReloadGun")
local fire = animations:WaitForChild("FireGun")
local hold = animations:WaitForChild("HoldGun")

--Animation Tracks
local reloadTrack
local fireTrack
local holdTrack

--Configurations
local range = configurations:WaitForChild("FireRange")
local allowTracing = configurations:WaitForChild("AllowTracing")
local ammo = configurations:WaitForChild("Ammo")

--Remote Events
local OnFire = remoteEvents:WaitForChild("OnFire")
local OnHit = remoteEvents:WaitForChild("OnHit")
local OnReload = remoteEvents:WaitForChild("OnReload")

--Remote functions
local AbleToReload = remoteEvents:WaitForChild("AbleToReload")
local AbleToShoot = remoteEvents:WaitForChild("AbleToShoot")

--Other Stuff
local sparkGui = shotgun.Flare:WaitForChild("SparkGui")

--Now we get to the other part of this. The functions.

local function equipGun()
    equipped = true

    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    if humanoid then
        pcall(function()
            holdTrack = humanoid:LoadAnimation(hold)
            holdTrack:Play()
        end)

        pcall(function()
            reloadTrack = humanoid:LoadAnimation(reload)
        end)

        pcall(function()
            fireTrack = humanoid:LoadAnimation(fire)
        end)
    end
end

local function unequipGun()
    equipped = false

    if holdTrack then
        holdTrack:Stop()
    elseif reloadTrack then
        reloadTrack:Stop()
    elseif fireTrack then
        fireTrack:Stop()
    end
end

local function reloadGun()
    if AbleToReload:InvokeServer() then

        OnReload:FireServer()

        if reloadTrack then
            reloadTrack:Play()
        end
    end
end

local function fireGun()
    if AbleToShoot:InvokeServer() then
        if fireTrack then
            fireTrack:Play()
        end
    end

    if ammo.Value > 0 then
        sparkGui.Enabled = true

        local ray = Ray.new(flare.CFrame.p, (mouse.hit.p - flare.CFrame.p).unit * range.Value)
        local touch, position = workspace:FindPartOnRay(ray, player.Character)

        if touch then
            OnHit:FireServer(touch)
        end
        OnFire:FireServer()

        if allowTracing.Value then
            local bullet = Instance.new("Part")
            bullet.Anchored = bullet
            bullet.CanCollide = false
            bullet.Transparency = 0.8
            bullet.BrickColor = BrickColor.White()
            bullet.Material = Enum.Material.SmoothPlastic

            local distance = (flare.CFrame.p - position).magnitude
            bullet.Size = Vector3.new(0, 0, distance)
            bullet.CFrame = CFrame.new(flare.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
            bullet.Parent = workspace

            game:GetService("Debris"):AddItem(bullet, 0.1)

            wait(0.1)

            sparkGui.Enabled = false
        end
    end
end

shotgun.Equipped:Connect(function()
    equipGun()

    mouse.Button1Up:Connect(function()
        mouseDown = false
    end)

    shotgun.Activated:Connect(function()
        mouseDown = true
        fireGun()
    end)

    ContextActionService:BindAction("Reload", reloadGun, false, Enum.KeyCode.R)
end)

shotgun.Unequipped:Connect(unequipGun)

Server-Side:

--Global Variables
local player
local shotgun = script.Parent.Parent
local handle = shotgun:WaitForChild("Handle")
local equipped = false
local reloading = false
local couldShoot = true

--Folders
local configurations = shotgun:WaitForChild("Configurations")
local remoteEvents = shotgun:WaitForChild("RemoteEvents")

--Configurations
local ammo = configurations:WaitForChild("Ammo")
local magSize = configurations:WaitForChild("MagSize")
local fireRate = configurations:WaitForChild("FireRate")
local damage = configurations:WaitForChild("Damage")
local headShotMultiplier = configurations:WaitForChild("HeadShotMultiplier")
local range = configurations:WaitForChild("FireRange")
local reloadTime = configurations:WaitForChild("ReloadTime")

--Remote Events
local onFire = remoteEvents:WaitForChild("OnFire")
local onHit = remoteEvents:WaitForChild("OnHit")
local onReload = remoteEvents:WaitForChild("OnReload")
local canFire = remoteEvents:WaitForChild("AbleToReload")
local canReload = remoteEvents:WaitForChild("AbleToShoot")

--Sounds
local emptySound = handle:WaitForChild("Empty")
local reloadSound = handle:WaitForChild("Reloading")
local fireSound = handle:WaitForChild("FireShots")

local function ableToReload(plr)
    if ammo.Value < magSize.Value then
        if not reloading then
            if equipped then
                return true
            end
        end
    end

    return false
end

local function reload(plr)
    if not ableToReload() then return end

    couldShoot = false
    reloading = true
    reloadSound:Play()
    wait(2)

    ammo.Value = magSize.Value

    reloading = false
    couldShoot = true
end

local function canShoot(plr, sound)
    if not reloading then
        if equipped then
            if ammo.Value > 0 then
                return true
            else
                if sound then
                    sound:Play()
                end
                return false
            end
        end
    end
end

local function shoot()
    if not canShoot() then return end
    if couldShoot then
        fireSound:Play()

        ammo.Value = ammo.Value - 1
    end
end

local function hit(plr, part)
    if not canShoot() then return end
    if not part then return end

    local character = plr.Character or plr.CharacterAdded:Wait()
    local rootPart = character:WaitForChild("HumanoidRootPart")

    if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then
        local humanoid
        local newDamage

        humanoid = part.Parent:FindFirstChild("Humanoid")

        if humanoid then
            newDamage = damage.Value

            if part.Name == "Head" then
                newDamage = damage.Value * headShotMultiplier.Value
            end

            humanoid:TakeDamage(newDamage)
        end
    end
end

local function equip()
    equipped = true
    player = game:GetService("Players"):GetPlayerFromCharacter(shotgun.Parent.Parent)
end

local function unequip()
    equipped = false
end

shotgun.Equipped:Connect(equip)
shotgun.Unequipped:Connect(unequip)
onHit.OnServerEvent:Connect(hit)
onFire.OnServerEvent:Connect(shoot)
onReload.OnServerEvent:Connect(reload)
canFire.OnServerInvoke = canShoot
canReload.OnServerInvoke = ableToReload

So basically, my gun is not reloading when it's at no ammunition, and it's not dealing damage to anything with a humanoid. Why is it doing that?

0
do you have a keybind set to R? for reload or smth idk? KronxGoat 50 — 4y
0
I used BindAction for this, reloading works fine, its just I can't reload when im at 0 ammunition torchmaster101 92 — 4y

Answer this question