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

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

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.

3 comments

0

There is no line 30...

0

Post full script please.

0

Sorry, I didn’t realize it renumbered the lines. I’ll put the full script in an answer.

2 answers

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.

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.

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