Code throwing weird errors that have no apparent source

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I reprogramed a part of my game to have a object spawn in and fly to the player and had the backend that gives money fire from there instead of when clicking an object, the error in question is "Workspace.Make Tree.tree.Money Collect Effect.move:16: attempt to index nil with ‘Money’ " (Or whatever the first line of code is after task.wait)

  1. What is the issue? Include screenshots / videos if possible!

My code works absolutely fine, Just it throws errors for no reason, I like keeping my console clean to make debugging easer.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I cant find any fix to my issue as most solutions are “your code has bad implementations”

local part = script.Parent
local pos = part.Position
local PDH = require(game.ServerScriptService.PlayerDataHandler)


-- Move 5 studs to the nearest player's position
local function moveToNearestPlayer()

	if MoveToPlayer then
		--move .1 studs towards the player + 1/10th of the distance left to the player's position
		local newPosition = part.Position + (MoveToPlayer.Position - part.Position).Unit * 1.07
		part.Position = newPosition
		--destroy this object if within 1 stud of the player's position
		if (MoveToPlayer.Position - part.Position).Magnitude < 1 then
			task.wait(.1)
			script.Parent.Money:Play()
			script.Parent.BillboardGui.Enabled = false
			script.Parent.Beam.Enabled = false
			local Earnings = script.Earnings
			local EarnClone = Earnings:Clone()
			EarnClone.displayCash.Enabled = true
			EarnClone.Name = tostring(script.Parent.DeliverAmount.Value)
			EarnClone.Parent = Plr.PlayerGui.Economy
			money.Value += script.Parent.DeliverAmount.Value
			PDH:Increment(Plr, "money", script.Parent.DeliverAmount.Value)
			task.wait(2)
			script.Parent.Money:Destroy()
			part:Destroy()
		end
	end
end
local function BackMoveToNearestPlayer()

	if MoveToPlayer then
		--move .1 studs towards the player + 1/10th of the distance left to the player's position
		local newPosition = (part.Position + (MoveToPlayer.Position - part.Position).Unit * -.1)
		part.Position = newPosition
		--destroy this object if within 1 stud of the player's position
		if (MoveToPlayer.Position - part.Position).Magnitude < 1 then
			part:Destroy()
		end
	end
end

script.Parent.BillboardGui.Enabled = true

ranMove1 = math.random(-1,1)
ranMove2 = math.random(.8,1.5)
ranMove3 = math.random(-1,1)

 		MoveToPlayerName = script.Parent.Parent.User.Value
		Plr = game.Players:WaitForChild(MoveToPlayerName)
		MoveToPlayer = Plr.Character:WaitForChild("HumanoidRootPart")
		script.Parent.Beam.Attachment1 = Plr.Character:WaitForChild("HumanoidRootPart"):WaitForChild("RootAttachment")

 	leaderstats = Plr:WaitForChild('leaderstats')
 	leaderstats.Name = "leaderstats"
	money = leaderstats:WaitForChild("Cash $")
	
	part.Parent = workspace
for i=47,0,-1 do
	
	task.wait(0.001)
	script.Parent.Position = script.Parent.Position + Vector3.new(ranMove1*i/200,ranMove2*i/200,ranMove3*i/200)*(MoveToPlayer.Position - part.Position).Magnitude/50
end
script.Parent.Beam.Enabled = true
for i=22,0,-1 do
	task.wait(0.001)
	BackMoveToNearestPlayer()
end
		
while true do
task.wait(0.001)

moveToNearestPlayer()
end

This is the code that causes the exact error I mentioned before

local part = script.Parent
local pos = part.Position
local PDH = require(game.ServerScriptService.PlayerDataHandler)


-- Move 5 studs to the nearest player's position
local function moveToNearestPlayer()

	if MoveToPlayer then
		--move .1 studs towards the player + 1/10th of the distance left to the player's position
		local newPosition = part.Position + (MoveToPlayer.Position - part.Position).Unit * 1.07
		part.Position = newPosition
		--destroy this object if within 1 stud of the player's position
		if (MoveToPlayer.Position - part.Position).Magnitude < 1 then
			task.wait(.1)
			script.Parent.BillboardGui.Enabled = false -- swapped this
			script.Parent.Money:Play() -- and this
			script.Parent.Beam.Enabled = false
			local Earnings = script.Earnings
			local EarnClone = Earnings:Clone()
			EarnClone.displayCash.Enabled = true
			EarnClone.Name = tostring(script.Parent.DeliverAmount.Value)
			EarnClone.Parent = Plr.PlayerGui.Economy
			money.Value += script.Parent.DeliverAmount.Value
			PDH:Increment(Plr, "money", script.Parent.DeliverAmount.Value)
			task.wait(2)
			script.Parent.Money:Destroy()
			part:Destroy()
		end
	end
end
local function BackMoveToNearestPlayer()

	if MoveToPlayer then
		--move .1 studs towards the player + 1/10th of the distance left to the player's position
		local newPosition = (part.Position + (MoveToPlayer.Position - part.Position).Unit * -.1)
		part.Position = newPosition
		--destroy this object if within 1 stud of the player's position
		if (MoveToPlayer.Position - part.Position).Magnitude < 1 then
			part:Destroy()
		end
	end
end

script.Parent.BillboardGui.Enabled = true

ranMove1 = math.random(-1,1)
ranMove2 = math.random(.8,1.5)
ranMove3 = math.random(-1,1)

 		MoveToPlayerName = script.Parent.Parent.User.Value
		Plr = game.Players:WaitForChild(MoveToPlayerName)
		MoveToPlayer = Plr.Character:WaitForChild("HumanoidRootPart")
		script.Parent.Beam.Attachment1 = Plr.Character:WaitForChild("HumanoidRootPart"):WaitForChild("RootAttachment")

 	leaderstats = Plr:WaitForChild('leaderstats')
 	leaderstats.Name = "leaderstats"
	money = leaderstats:WaitForChild("Cash $")
	
	part.Parent = workspace
for i=47,0,-1 do
	
	task.wait(0.001)
	script.Parent.Position = script.Parent.Position + Vector3.new(ranMove1*i/200,ranMove2*i/200,ranMove3*i/200)*(MoveToPlayer.Position - part.Position).Magnitude/50
end
script.Parent.Beam.Enabled = true
for i=22,0,-1 do
	task.wait(0.001)
	BackMoveToNearestPlayer()
end
		
while true do
task.wait(0.001)

moveToNearestPlayer()
end

This is the exact same code just with 2 lines flipped and it gives the error “Workspace.Make Tree.tree.Money Collect Effect.move:16: attempt to index nil with ‘BillboardGui’”

I have no Idea why it’s doing this, as I’m not attempting to index anything, and it still does everything else just fine. even play the sound. the error is only thrown after the 2nd task.wait() after its wait time.

I forgot to paste it, but here’s a video of what’s going on

(may still be processing)

Problem:
The first iteration of the loop works as intended but then an error occurs during the second iteration (essentially saying that script.Parent is null/doesn’t exist).

Explanation:
It looks like script.Parent is assigned to the variable part.
This line here: script.Parent.BillboardGui.Enabled = false is giving the error.
However, the line can be rewritten as part.BillboardGui.Enabled.
The last thing you do in this loop is destroy part.
Here comes the second iteration, but now part is no longer alive.
You try using it, but now its null and it spits out an error.

Thanks, I realized there was an Unneeded :Destroy() in my BackMoveToNearestPlayer() function

I also changed my final while loop to be

while script.Parent do
moveToNearestPlayer()
task.wait(0.001)
end

As the error happened because it executed after the part had been broken.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.