Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

3 comments

0

20:52:52.215 - Players.Player1.Backpack.Pickaxe.LocalScript:4: attempt to index local 'c' (a nil value)

0

ahhh messy code

0

Good for giving the entire error in comments, bravo!

1 answer

✓ Accepted answer

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.

1 comment

0

Just do: local c = plr.Character or plr.CharacterAdded:wait()

This discussion is preserved as it was. New answers, comments, and votes are closed.