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

I get this error when i shoot on nil pls send me only answers how to fix?

Asked by
kiref81 -24
2 years ago
Edited 2 years ago

I made a pistol but when i shoot in nil i get this error = Players.kiref81.Backpack.Pistol.Pistol:61: attempt to index nil with 'Parent'

local gun = script.Parent
local runService = game:GetService('RunService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local player = game:GetService('Players')

local viewModel = replicatedStorage:WaitForChild('MainGame'):WaitForChild('Guns'):WaitForChild('Pistol'):Clone()
local camera = workspace.CurrentCamera
local player = player.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local h = character:WaitForChild('Humanoid') or character:FindFirstChild('Humanoid') or nil
viewModel.Parent = replicatedStorage.UnequippedGuns

local config = require(viewModel:WaitForChild('Config'))

local cf = CFrame.new()

local sinSpeed = 2.5
local sinSize = 3
local cosSpeed = 4
local cosSize = 4
local recoilSpeed = 8
local recoilSize = .5


local equipConnection

local shoot_Cooldown = false

local animations = {
    ['Hold'] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Hold);
}

if h ~= nil then
    animations['CharacterHold'] = h:LoadAnimation(viewModel.CharacterAnimations.Hold);
end

cf = config.Offset_From_Camera

function positionModel()
    if player.character then
        viewModel:SetPrimaryPartCFrame(camera.CFrame*cf)
        local sin = math.sin(time() * sinSpeed)/sinSize
        local cos = math.cos(time() * cosSpeed)/cosSize 
        if player.character.Humanoid.MoveDirection.Magnitude > 0 then
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X+sin, config.Offset_From_Camera.Y+cos,config.Offset_From_Camera.Z),.2)   
        else
            cf = cf:Lerp(config.Offset_From_Camera,0.2)
        end
    end
end

function shoot(target)
    if not shoot_Cooldown then       
        shoot_Cooldown = true
        if player.Character:FindFirstChild(script.Parent.Name) then
            local sin = math.sin(time() * recoilSpeed)/recoilSize
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X, config.Offset_From_Camera.Y,config.Offset_From_Camera.Z+sin),.2)
            viewModel.Shoot:Play()
        end
        if target.Parent:FindFirstChild('Humanoid') then
            replicatedStorage['MainGame']['Remotes']['Shoot']:FireServer(mouse.Target, gun.Name)
        end
        wait(config.CoolDown_Between_Each_Click)
        shoot_Cooldown = false
    end
end

gun.Activated:Connect(function()
    shoot(mouse.Target)
end) 
local function equip()
    if not gun and viewModel then return end
    for _, part in pairs(gun:GetChildren()) do
        if part:IsA('BasePart') or part:IsA('UnionOperation') then
            part.Transparency = 1
        end
    end
    viewModel.Parent = camera
    animations['Hold']:Play()
    if h ~= nil then
        animations['CharacterHold']:Play()
    end
    equipConnection = runService.RenderStepped:Connect(function()
        if player.Character.Humanoid.Health > 0 then
            positionModel()
        end
    end)
end

local function unequip()
    if not gun then return end
    equipConnection:Disconnect()
    viewModel.Parent = replicatedStorage.UnequippedGuns
    if h ~= nil then
        animations['CharacterHold']:Stop()
    end
end

gun.Equipped:Connect(function()
    mouse.Icon = "http://www.roblox.com/asset?id=7269154410"
    equip()
end)

gun.Unequipped:Connect(function()
    unequip()
end)
0
Hello, could you edit your question so that the code is in between the ~? Neatwyy 123 — 2y

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
2 years ago

in your shoot() function i do see a potential problem with your mouse.Target system. you have the following:

function shoot(target) 
    if not shoot_Cooldown then
        shoot_Cooldown = true 
        if player.Character:FindFirstChild(script.Parent.Name) then 
            local sin = math.sin(time() * recoilSpeed)/recoilSize 
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X, config.Offset_From_Camera.Y,config.Offset_From_Camera.Z+sin),.2)
            viewModel.Shoot:Play() 
        end 
        if target.Parent:FindFirstChild('Humanoid') then  -- this could break!
            replicatedStorage['MainGame']['Remotes']['Shoot']:FireServer(mouse.Target, gun.Name) 
        end
        wait(config.CoolDown_Between_Each_Click) 
        shoot_Cooldown = false 
    end 
end

Replace the above function with this modified version.(this does the same but it has a few little checks to make sure you dont get the dreded try to index parent with nil error.

function shoot(target) 
    if not shoot_Cooldown then
        shoot_Cooldown = true 
        if player.Character:FindFirstChild(script.Parent.Name) then 
            local sin = math.sin(time() * recoilSpeed)/recoilSize 
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X, config.Offset_From_Camera.Y,config.Offset_From_Camera.Z+sin),.2)
            viewModel.Shoot:Play() 
        end 
        if(target ~= nil) then
            if(target.Parent ~= nil) then
                if target.Parent:FindFirstChild('Humanoid') then 
                    replicatedStorage['MainGame']['Remotes']['Shoot']:FireServer(mouse.Target, gun.Name) 
                end 
            end
        end
        wait(config.CoolDown_Between_Each_Click) 
        shoot_Cooldown = false 
    end 
end

If this doesn't fix the problem, then it might be the way you've structured your variables from line 1

try:

Replace this:

local viewModel = replicatedStorage:WaitForChild('MainGame'):WaitForChild('Guns'):WaitForChild('Pistol'):Clone() 

With

local MainGme = replicatedStorage:WaitForChild('MainGame',10)
local GunsFolder = MainGme:WaitForChild('Guns',10)
local Pistol = GunsFolder:WaitForChild('Pistol',10)
local viewModel = Pistol:Clone() 

Bunching the WaitForChild() on one line makes it easy for the script to break and makes it hard to de-bug. Also, if you're wondering what the 10 number is for on the end of the WaitForChild() function that's simply a timeout (in seconds)

Code in full (With modifcations!)

local gun = script.Parent 
local runService = game:GetService('RunService') 
local replicatedStorage = game:GetService('ReplicatedStorage') 
local player = game:GetService('Players')
local MainGme = replicatedStorage:WaitForChild('MainGame',10)
local GunsFolder = MainGme:WaitForChild('Guns',10)
local Pistol = GunsFolder:WaitForChild('Pistol',10)
local viewModel = Pistol:Clone() 
local camera = workspace.CurrentCamera 
local player = player.LocalPlayer 
local mouse = player:GetMouse() 
local character = player.Character or player.CharacterAdded:Wait() 
local h = character:WaitForChild('Humanoid') or character:FindFirstChild('Humanoid') or nil viewModel.Parent = replicatedStorage.UnequippedGuns
local config = require(viewModel:WaitForChild('Config'))
local cf = CFrame.new()
local sinSpeed = 2.5 
local sinSize = 3 
local cosSpeed = 4 
local cosSize = 4 
local recoilSpeed = 8 
local recoilSize = .5
local equipConnection
local shoot_Cooldown = false
local animations = {['Hold'] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Hold);}
if h ~= nil then 
    animations['CharacterHold'] = h:LoadAnimation(viewModel.CharacterAnimations.Hold); 
end
cf = config.Offset_From_Camera
function positionModel() 
    if player.character then 
        viewModel:SetPrimaryPartCFrame(camera.CFrame*cf) 
        local sin = math.sin(time() * sinSpeed)/sinSize 
        local cos = math.cos(time() * cosSpeed)/cosSize 
        if player.character.Humanoid.MoveDirection.Magnitude > 0 then 
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X+sin, config.Offset_From_Camera.Y+cos,config.Offset_From_Camera.Z),.2)
        else 
            cf = cf:Lerp(config.Offset_From_Camera,0.2) 
        end 
    end 
end
function shoot(target) 
    if not shoot_Cooldown then
        shoot_Cooldown = true 
        if player.Character:FindFirstChild(script.Parent.Name) then 
            local sin = math.sin(time() * recoilSpeed)/recoilSize 
            cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X, config.Offset_From_Camera.Y,config.Offset_From_Camera.Z+sin),.2)
            viewModel.Shoot:Play() 
        end 
        if(target ~= nil) then
            if(target.Parent ~= nil) then
                if target.Parent:FindFirstChild('Humanoid') then 
                    replicatedStorage['MainGame']['Remotes']['Shoot']:FireServer(mouse.Target, gun.Name) 
                end 
            end
        end
        wait(config.CoolDown_Between_Each_Click) 
        shoot_Cooldown = false 
    end 
end
gun.Activated:Connect(function() 
    shoot(mouse.Target) 
end) 
local function equip() 
    if not gun and viewModel then 
        return 
    end 
        for _, part in pairs(gun:GetChildren()) do 
        if part:IsA('BasePart') or part:IsA('UnionOperation') then 
            part.Transparency = 1 
        end 
    end 

    viewModel.Parent = camera 
    animations['Hold']:Play() 
    if h ~= nil then 
        animations['CharacterHold']:Play() 
    end 
    equipConnection = runService.RenderStepped:Connect(function() 
        if player.Character.Humanoid.Health > 0 then 
            positionModel() 
        end 
    end) 
end

local function unequip() 
    if not gun then 
        return 
    end 
    equipConnection:Disconnect() 
    viewModel.Parent = replicatedStorage.UnequippedGuns 

    if h ~= nil then 
        animations['CharacterHold']:Stop() 
    end 
end

gun.Equipped:Connect(function() 
    mouse.Icon = "http://www.roblox.com/asset?id=7269154410" 
    equip() 
end)

gun.Unequipped:Connect(function() 
    unequip() 
end)

Hope this helps!. Also, couldn't find the error you was getting due to the formatting of this question, use this sites Lua Code Block (the button that's a blue circle/bubble thing with the word Lua inside button) the ~ for formatting code. So it's easier to read

Ad

Answer this question