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

Gun Problem - Hit_Player is a nil value? [closed]

Asked by
KordGamer 155
9 years ago

I'm making a game and I was making a gun. This is the script.

wait(1)

local Seat = script.Parent

local MG = Seat.Parent
local Vehicle = MG.Parent.Parent

local W_X, W_Y = Seat.X, Seat.Y

local Events = script.Events

local SeatedWeld
local tool




Seat.ChildAdded:connect(function(weld)
    if weld.Name == "SeatWeld" and weld:IsA("Weld") then
        player = game.Players:GetPlayerFromCharacter(weld.Part1.Parent)
        if not player then
            return
        end
        SeatedWeld = weld
        tool = script["MG42"]:Clone()
        tool.Events.Value = Events
        tool.Parent = player.Backpack

-- WAT

local Camera = Workspace.CurrentCamera

local Tool = MG.Parent.MG

Settings = {
    Range = 10000
    ,Spread = 7
    ,Damage = {20, 20}
    ,HeadShotDamage = {40, 40}
    ,Bullets = 1
    ,FireRate = 1
    ,Automatic = true
    ,AntiTK = true
    ,ImpactDuration = 3
    --Reload
    ,ReloadKey = "R"
    ,ReloadTime = 2.7
    ,Ammo = 50
    ,IncludeChamberedBullet = true
    --Gui
    ,GuiPos = Vector2.new(script.Gui.Frame.Position.X.Scale, script.Gui.Frame.Position.Y.Scale)
    --Arms
    ,OneHanded = false
    ,FakeArms = true
    ,FakeArmTransparency = 0
    ,RightPos = CFrame.new(-0.75, -0.25, 0.5) * CFrame.Angles(math.rad(-90), 0, 0)
    ,LeftPos = CFrame.new(1, 0.5, 0.5) * CFrame.Angles(math.rad(-90), math.rad(45), 0)
    --,AimPart = Tool.AimPart
    ,AimPartOffset = CFrame.new()
    ,AimGripOffset = Vector3.new(0, 0, 0)
    ,LeftAimPos = CFrame.new(1, 0.5, 0.5) * CFrame.Angles(math.rad(-100), math.rad(45), 0)
    ,AimAnim = {
        Enabled = false
        ,Frames = 50
        ,Time = 0.1
    }
    ,ChangeFOV = {true, 60}
    ,SpreadReduction = 0
    --Weld
    ,Weld = true
    --Mouse
    ,Icons = {
        Idle = "rbxassetid://70288585"
        ,Fire1 = "rbxassetid://70288578"
        ,Fire2 = "rbxassetid://70288572"
        ,Fire3 = "rbxassetid://70288578"
        ,Reload = "rbxassetid://70288599"
        ,Aim = "rbxasset://textures/Blank.png"
    }
    ,FireMouseAnimTime = 0.15
    --Recoil
    ,Recoil = math.rad(1)
    --Smoke
    ,Smoke = false
    --,SmokePart = Tool.SmokePart
    ,Smokes = {{script.Smoke, 6, 0.1}, {script.Smoke1, 0.1}, {script.Smoke1, 0.1}}
    --Sprint
    ,Sprint = true
    ,SprintSpeed = 25
    ,SprintKey = "0"
    ,SprintSpreadIncrease = 40
    ,SprintArmOffset = CFrame.new(0.25, 0.125, 0.25) * CFrame.Angles(math.rad(-250), math.rad(35), 0)
    --Other
    ,DropHats = false
}

--Do not edit below this lign--

local Orig_Spread = Settings.Spread

Settings.Range = math.min(Settings.Range, 999)

local Ignore_Model = Workspace:FindFirstChild("Ray_Ignore") or Instance.new("Model", Workspace)
Ignore_Model.Name = "Ray_Ignore"

local Ray_Ignore = {Ignore_Model, Workspace.CurrentCamera, Vehicle}

Ammo = Settings.Ammo
AmmoDisplay = MG.Parent.MG.AmmoDisplay.GUI.Display
AmmoDisplay.Text = Ammo

function Fire()
    if Ammo <= 0 then
        AmmoDisplay.Text = "EMPTY"
        return
    end
    Ammo = Ammo - 1 
    AmmoDisplay.Text = Ammo
    if MG.Parent.MG.Body:FindFirstChild("Shoot") then
        MG.Parent.MG.Body.Shoot:Play()
    end
    local flash = script.Smoke1:Clone()
    flash.Parent = MG.Parent.MG.FlashPos
    flash.Enabled = true
    game.Debris:AddItem(flash, 0.07)
    local Spread = CFrame.Angles(math.rad(math.random(-Settings.Spread, Settings.Spread)/10), math.rad(math.random(-Settings.Spread, Settings.Spread)/10), math.rad(math.random(-Settings.Spread, Settings.Spread)/10))
    local Ray = Ray.new(MG.Parent.MG.FlashPos.Position, ((MG.Parent.MG.FlashPos.CFrame * CFrame.new(0, 0, 1)) * Spread).lookVector.unit * Settings.Range)
    local Hit, Pos = Workspace:FindPartOnRayWithIgnoreList(Ray, Ray_Ignore)
    if Hit and Hit.Parent:FindFirstChild("Humanoid") then
        local Hit_Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Settings.AntiTK and not Hit_Player.Neutral and Hit_Player.TeamColor == player.TeamColor then
            return
        end
        local Humanoid = Hit.Parent.Humanoid
        local c = Instance.new("ObjectValue")
        c.Name = "creator"
        c.Value = player
        game.Debris:AddItem(c, 3)
        c.Parent = Humanoid
        if Hit.Name == "Head" then
            Humanoid.Health = Humanoid.Health - math.random(Settings.HeadShotDamage[1], Settings.HeadShotDamage[2])
        else
            Humanoid.Health = Humanoid.Health - math.random(Settings.Damage[1], Settings.Damage[2])
        end
    elseif Hit then
        local Hit_Part = Instance.new("Part", Ignore_Model)
        local Mesh_Part = game.Lighting.Arrow:Clone()
        Mesh_Part.Parent = Hit_Part
        Hit_Part.FormFactor = "Custom"
        Hit_Part.TopSurface = 0
        Hit_Part.BottomSurface = 0
        Hit_Part.Anchored = true
        Hit_Part.CanCollide = false
        Hit_Part.Size = Vector3.new()
        Hit_Part.CFrame = CFrame.new(Pos) * CFrame.Angles(math.random(0, 360), math.random(0, 360), math.random(0, 360))
        Hit_Part.BrickColor = BrickColor.new("Brown")
        game.Debris:AddItem(Hit_Part, Settings.ImpactDuration)
        if not Hit.Anchored then
            local w = Instance.new("Weld", Hit_Part)
            w.Part0 = Hit
            w.Part1 = Hit_Part
            w.C0 = Hit.CFrame:toObjectSpace(Hit_Part.CFrame)
            Hit_Part.Anchored = false
        end
        if Hit.Parent:IsA("Hat") and Settings.DropHats then
            Hit.Parent.Parent = workspace
            game.Debris:AddItem(Hit, 3)
        end
    end
end


--WAT
    end
end)

Seat.ChildRemoved:connect(function(weld)
    if weld == SeatedWeld and tool then
        Events.Deselecting.Value = true
        wait()
        tool:Destroy()
        Events.Deselecting.Value = false
        tool = nil
    end
end)

local Keys_Down = {}

local Firing = false

Events.KeyDown.Changed:connect(function()
    if Events.KeyDown.Value ~= "" then
        Keys_Down[Events.KeyDown.Value:upper()] = true
        Events.KeyDown.Value = ""
        if Keys_Down["R"] and not Firing and not Reloading then
            Reloading = true
            AmmoDisplay.Text = "RELOADING"
            MG.Parent.MG.Body.Reload:Play()
            wait(Settings.ReloadTime)
            Ammo = Settings.Ammo
            AmmoDisplay.Text = Ammo
            Reloading = false
        end
    end
end)

Events.KeyUp.Changed:connect(function()
    if Events.KeyUp.Value ~= "" then
        Keys_Down[Events.KeyUp.Value:upper()] = false
        Events.KeyUp.Value = ""
    end
end)

local MouseHeld = false


Events.MouseHeld.Changed:connect(function()
    MouseHeld = Events.MouseHeld.Value
    if not Firing and Ammo > 0 and not Reloading then
        Firing = true
        while MouseHeld and tool do
            local out = Fire()
            if out then
                break
            end
            wait(Settings.FireRate)
        end
        Firing = false
    end
end)

local MouseHit = Events.MouseHit
local Origin = MouseHit.Origin

MouseHit.Value = Seat.CFrame + Seat.CFrame.lookVector * 1000
Origin.Value = Seat.CFrame

local Yaw = 0
local Pitch = 0

local TurnJoint = Vehicle:FindFirstChild(MG.Parent.Name .. "TurnJoint")

while wait() do
    if tool then
        local look = TurnJoint.CFrame:vectorToObjectSpace(MouseHit.Value.p - Origin.Value.p).unit
        Yaw, Pitch = math.atan2(look.x, -look.z), math.asin(look.y)
        Yaw = math.max(math.rad(-120), math.min(math.rad(120), Yaw))
        Pitch = math.max(math.rad(-10), math.min(math.rad(40), Pitch))
        W_Y.C0 = CFrame.Angles(-Pitch, 0, 0)
        W_X.C0 = CFrame.Angles(0, -Yaw, 0)
    end
end

and this is the error : 21:29:58.383 - Workspace.Model.RearMG.MGFrame.MGSeat.Gun_Script:131: attempt to index local 'Hit_Player' (a nil value) 21:29:58.384 - Stack Begin 21:29:58.385 - Script 'Workspace.Model.RearMG.MGFrame.MGSeat.Gun_Script', Line 131 - global Fire 21:29:58.385 - Script 'Workspace.Model.RearMG.MGFrame.MGSeat.Gun_Script', Line 222 21:29:58.386 - Stack End

I have no clue how to fix it, Could you shed some light?

The main problem is with line 131.

Closed as Not Constructive by UserOnly20Characters and Unclear

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 9 years ago

What exactly is neutral? a team? Unless Neutral is a child of Player the system will return the error you are getting since Hit_Player.Neutral doesn't exist

try this

if Settings.AntiTK and not Hit_Player.TeamColor == BrickColor.new("White") and Hit_Player.TeamColor == player.TeamColor then
            return
end

--Make sure BrickColor.new("White") is equivelent to the neutral team, team color

Ad
Log in to vote
0
Answered by
KordGamer 155
9 years ago

Oh, Guys.. It's not that line that is the problem, The problem is Hit_Player.

local Hit_Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
0
Ok, Sorry guys! I fixed the problem! KordGamer 155 — 9y