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

"Workspace.Blox.BloxScript:30: attempt to index a nil value." Error issues?

Asked by 5 years ago
Blox.Touched:Connect(function(touch)
    if CanTouch.Value == true then
        CanTouch.Value = false
        Torso = touch.Parent:FindFirstChild("Torso")
        if Torso ~= nil then
            local Player = Torso.Parent
            Blox.CanCollide = false
            local PlayerName = Player.Name
            local BloxStat = game.Players:FindFirstChild(PlayerName):FindFirstChild("leaderstats"):FindFirstChild("BloxCounter")
                BloxStat.Value = BloxStat.Value + 1
                Ghostify()
        end
    end
end)

The above code is for the collectibles in my game which I call "Blox." But for some reason, I keep getting the error: Workspace.Blox.BloxScript:30: attempt to index a nil value. I believe its coming from the end of the line which says "FindFIrstChild("BloxCounter")" but a value is named that from the start of the game so it shouldn't be nil. I really don't know what else could be going on.

0
There is no line 30... User#19524 175 — 5y
0
Post full script please. RAYAN1565 691 — 5y
0
Sorry, I didn’t realize it renumbered the lines. I’ll put the full script in an answer. tygerupercut3 68 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
local Blox = script.Parent
local Pos = Blox.Position
local BodyPosition = Blox.BodyPosition
local BodyPositionStopped = game.Workspace.BodyPosition
local PlayerValue = Blox.PlayerValue
--Players = game:GetService("Players")
local CanTouch = Blox.CanTouch

function Ghostify()
    local i = 0
    repeat
        BodyPosition.MaxForce = BodyPositionStopped.MaxForce
        BodyPosition.Position = Torso.Position
        Blox.Transparency = Blox.Transparency + 0.05
        wait(0.05)
        i = i + 1
    until i == 15
    Blox:Destroy()
end

Blox.Touched:Connect(function(touch)
    if CanTouch.Value == true then
        CanTouch.Value = false
        Torso = touch.Parent:FindFirstChild("Torso")
        if Torso ~= nil then
            local Player = Torso.Parent
            if Player.Name ~= PlayerValue.Value then
                Blox.CanCollide = false
                local PlayerName = Player.Name
                local BloxStat = game.Players:FindFirstChild(PlayerName).leaderstats.BloxCounter
                BloxStat.Value = BloxStat.Value + 1
                Ghostify()
            end
        end
    end
end)

Line 30 is the one that sets BloxStat to BloxCounter. Also the ghostly function is there to add a smooth collecting animation so it looks like the collectible floats into your player instead of disappearing instantly.

Ad
Log in to vote
0
Answered by
exvient 30
5 years ago

Use a remote event instead, and have another script handle the remote event function to award it to the player in leaderstats. That way no exploiters can manipulate it and you can edit and potentially fix the script in the future.

Answer this question