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

My gun script is broken sometimes?, and output says attempt to index nil with "FindFirstChild"

Asked by 1 year ago
Edited 1 year ago
local debounce = false

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,hit,target)
    if debounce == false then
        debounce = true
        local newpart = Instance.new("Part",workspace)
        local dist = (hit-player.Character.HumanoidRootPart.Position).Magnitude
        newpart.BrickColor = BrickColor.new("Fire Yellow")
        newpart.Anchored = true
        newpart.CanCollide = false
        newpart.Size = Vector3.new(0.2,0.2,(hit-script.Parent.Handle.Position).Magnitude)
        newpart.CFrame = CFrame.new(script.Parent.Handle.Position,hit) * CFrame.new(0,0,-dist/2)
        script.Parent.Handle["Glock 21 Gunshot"]:Play()
        spawn(function()
            for i = 1,10 do
                newpart.Transparency = i/10
                if i == 10 then
                    newpart:Destroy()
                end
                wait(0.05)
            end
        end)

        if target ~= nil then
            if target.Parent:FindFirstChild("Humanoid") then
                if target.Parent ~= player.Character then
                    target.Parent.Humanoid:TakeDamage(script.Parent.damage.Value)
                    end
            end
end
        wait(script.Parent.Firerate.Value)
        debounce = false
    end
end)
while wait() do
    script.Parent.Firerate.Value = script.Parent.OriFirerate.Value - (script.Parent.OriFirerate.Value/100*game.Workspace.FirerateMul.Value)
    script.Parent.damage.Value = script.Parent.Oridamage.Value + (script.Parent.Oridamage.Value/100*game.Workspace.DamageMul.Value)
end
0
in line 24, change it to `if target ~= nil and target.Parent ~= nil then` T3_MasterGamer 2189 — 1y
0
it's because even tho `target` exists, there's a chance that its parent doesn't exist, meaning `target` has been deleted T3_MasterGamer 2189 — 1y
0
`target.Parent ~= nil` checks if `target` hasn't been destroyed T3_MasterGamer 2189 — 1y

Answer this question