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

Gun script not working, nothing happens when you use the tool?

Asked by 4 years ago
Edited 4 years ago

For some reason this script doesn't work.

--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local debounce = true
local config = tool.Config
local range = config.Range
local dmg = config.Damage
local coolDown = config.CoolDown
local clips = config.Clips
local ammo = config.Ammo
local maxAmmo = config.MaxAmmo
local allowTracing = config.AllowTracing
local reloadTime = config.ReloadTime
local isReloading = false
--//Sounds
local reloadSound = tool.Handle.ReloadSound
local fireSound = tool.Handle.FireSound
local equipSound = tool.Handle.EquipSound
local emptySound = tool.Handle.EmptySound

--//Events
tool.Equipped:connect(function(mouse)
    equipSound:Play()
    tool.Activated:connect(function()
        if not isReloading then
            if ammo.Value > 0 then
                if debounce then
                    --//Play sound
                    fireSound:Play()
                    --//Doesn't allow spamming
                    debounce = false
                    --//Ray Get, Set
                    local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p) * range.Value)
                    local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)

                    --//Bullet Tracing
                    if allowTracing.Value == true then
                        --//Make part
                        local trace = Instance.new("Part", workspace)
                        trace.Material = Enum.Material.Neon
                        trace.BrickColor = BrickColor.new("Black")
                        trace.CanCollide = false
                        trace.Anchored = true
                        trace.Transparency = 0.5

                        --//Show Direction
                        local distance = (hole.CFrame.p - position).magnitude
                        trace.Size = Vector3.new(0.2, 0.2, distance)
                        trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)

                        --//Remove debris
                        game:GetService("Debris"):AddItem(trace, 0.1)
                    end

                    --//Hit Detection
                    if hit then
                        local humanoid =  hit.Parent:FindFirstChild("Humanoid")
                        if humanoid then
                            if hit.Name == "Head" then
                                --//Double damage on headshots
                                humanoid:TakeDamage(dmg.Value*2)
                            else
                                --//Normal Damage on body shots
                                humanoid:TakeDamage(dmg.Value)
                            end
                        end
                    end
                    ammo.Value = ammo.Value - 1
                    wait(coolDown.Value)
                    debounce = true
                end
            else
                --//Out of ammo
                emptySound:Play()
            end
        end
    end)

    --//Reload Event
    mouse.KeyDown:connect(function(key)
        key:lower()
        if key == "r" then
            if not isReloading then
                if clips.Value > 0 then
                    reloadSound:Play()
                    isReloading = true
                    ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    wait(reloadTime.Value)
                    isReloading = false
                end
            end
        end
    end)
end)

Answer this question