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

What do I replace Parent with to prevent roblox studio from thinking it is nil?

Asked by 4 years ago
Edited 4 years ago

For my gun, I put in a trajectory block with hit.Parent but there is an error around line 35.

FYI the script is in ServerScriptService.

Error: ServerScriptService.MainScript:35: attempt to index field 'Parent' (a nil value)

The part being referred is the object being hit by the gun.

Also, I got the suggestion that :FindPartOnRay() can return nil parts but idk how to recode anything

Part of the Code:

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local kills = "Kills"
local damage = 10
replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
    if game.Workspace[player.Name].Humanoid.Health <= 0 then
        -- Nothing Happens
    else
        local distance = (tool.Handle.CFrame.p - position).magnitude
        if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then 
            game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()
        end
        local trajectory = Instance.new("Part",game.Workspace)
        local smoke = serverStorage.SmokeParticle:Clone()
        smoke.Parent = tool.Handle
        trajectory.BrickColor = BrickColor.new("Dark blue")
        trajectory.Material = "SmoothPlastic"
        trajectory.Name = player.Name.."'s Trajectory"
        trajectory.Transparency = 0.5
        trajectory.Anchored = true
        trajectory.Locked = true
        trajectory.CanCollide = false
        trajectory.Size = Vector3.new(0.3,0.3,distance)
        for i = 0, distance,6 do
            trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2)
            wait(0.0001)
        end
        smoke:Destroy()
        if part then 
            if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
                -- Headshot
                replicatedStorage.Headshot:FireClient(player)
                damage= 20
            end
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            else
                humanoid:TakeDamage(damage)
                --if humanoid.Health == 0 then
                    --player.leaderstats[kills].Value = player.leaderstats[kills].Value + 1
                --end
            end
            wait(0.25)
            if trajectory then
                trajectory:Destroy()
            end
        end
    end 
end)


Also my local script inside gun in workspace (with fireEvent)

tool.Equipped:Connect(function(mouse)
    gungui:Clone().Parent = player.PlayerGui
    findBodyType()
    equipAnimation:FireServer(tool.shoot)
    mouse.Icon = "http://www.roblox.com/asset?id=42445293"
    mouse.Button1Down:Connect(function()
        if bullets.Value <=0 or reloading == true then
            -- Don't Do Anything
        else
            local head = game.Workspace[player.Name].Head.CFrame.lookVector
            local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
            difference = (head-mouse)
            local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*300)
            local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)

            if difference.magnitude < 1.33 then
                sound:Play()
                shootevent:FireServer(tool,position,part)
                bullets.Value = bullets.Value - 1
            end
        end
    end)
0
Please use the Lua Code block. It's very hard to read this. https://scriptinghelpers.org/help/how-post-good-questions-answers [Scroll To The Bottom] killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Try this:

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local kills = "Kills"
local damage = 10
replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
    if game.Workspace[player.Name].Humanoid.Health <= 0 then
        -- Nothing Happens
    else
        local distance = (tool.Handle.CFrame.p - position).magnitude
        if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then 
            game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()
        end
        local trajectory = Instance.new("Part",game.Workspace)
        local smoke = serverStorage.SmokeParticle:Clone()
        smoke.Parent = tool.Handle
        trajectory.BrickColor = BrickColor.new("Dark blue")
        trajectory.Material = "SmoothPlastic"
        trajectory.Name = player.Name.."'s Trajectory"
        trajectory.Transparency = 0.5
        trajectory.Anchored = true
        trajectory.Locked = true
        trajectory.CanCollide = false
        trajectory.Size = Vector3.new(0.3,0.3,distance)
        for i = 0, distance,6 do
            trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2)
            wait(0.0001)
        end
        smoke:Destroy()
        if part then 
            if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
                -- Headshot
                replicatedStorage.Headshot:FireClient(player)
                damage= 20
            end
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            if not part.Parent:FindFirstChild("Humanoid") then
                -- cant do anything with a non-humanoid object
            elseif humanoid then -- if we find a humanoid then damage
                humanoid:TakeDamage(damage)
                --if humanoid.Health == 0 then
                    --player.leaderstats[kills].Value = player.leaderstats[kills].Value + 1
                --end
            end
            wait(0.25)
            if trajectory then
                trajectory:Destroy()
            end
        end
    end 
end)

The reason is because your trying to execute :TakeDamage() on a nil value.

0
Thank you for your time, I appreciate it! I actually set up a similar condition to the one you posted to prevent the nil from popping up. Resolved the problem before I read this. Mbattleaxe -3 — 4y
0
Nice to hear, and make sure to always know that roblox functions start with a colon not with a period, but you get used to it! 123nabilben123 499 — 4y
Ad

Answer this question