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

'c' (a nil value) (Character) (?)

Asked by 6 years ago
Edited 6 years ago

Localscript in tool

local tool = script.Parent
local plr = tool.Parent.Parent
local mouse = plr:GetMouse()
local Crystals = game.Workspace.Crystals
local c = plr.Character --here
local hum = c.Humanoid --here

local storage = game.ReplicatedStorage.CrystalStorage

local RegenTime = math.random(5,10)

local period = math.random(4,15)
game.Workspace.Area.Touched:connect(function(plr)
local function onActivate()
 if mouse.Target.Name == "Crystal" then
    mouse.Target.Owner.Value = c
        local a = hum:LoadAnimation(tool.Handle.Animation)
        a:Play()
        wait(period)
        a:Stop()
    for i,a in pairs(game.Workspace.Crystals:GetChildren()) do
            if a:IsA("UnionOperation") then
                if a.Owner.Value == c then
                    a.Parent = storage
                    wait(RegenTime)
                    a.Parent = Crystals
                    a.Owner.Value = nil
                end
            end
    end
    end
end


tool.Activated:connect(onActivate)
end)

0
20:52:52.215 - Players.Player1.Backpack.Pickaxe.LocalScript:4: attempt to index local 'c' (a nil value) SamthekidRSsl 58 — 6y
0
ahhh messy code hiimgoodpack 2009 — 6y
0
Good for giving the entire error in comments, bravo! User#18718 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

My best guess as to why variable c is returning a nil value is because of certain quirks with accessing the character from the LocalScript. You can read more about that here. It looks like when this script is run the character is not created yet (or that's my best guess).

What I would suggest is not referencing the character and the player by script.Parent references. This makes it harder to understand. We can also steal advice from the article and rewrite your top portion to read.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local Crystals = game.Workspace.Crystals
local c = player.Character
if not c or not c.Parent then -- If c is a model that no longer has a parent ...
    c = plr.CharacterAdded:wait()
end

That might be the answer you are looking for.

0
Just do: local c = plr.Character or plr.CharacterAdded:wait() KingLoneCat 2642 — 6y
Ad

Answer this question