Cant find fix error but working properly

I keep getting findfirstchild errors, and i feel like im starting to mess up the logic and i hope someone can help me find out what i did wrong because i just cant seem to find it

also im hoping to know how to improve this script since its kinda messy and looks wrong

this is a part of a local script

UserInputService.InputEnded:Connect(function(InputObject, GameProcessed)
	if not IsPlacing then
		if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and GameProcessed == false then
			local RaycastResult = MouseRaycast("Include", {game.Workspace.CurrentUnits})
			if RaycastResult and RaycastResult.Instance and RaycastResult.Instance.Parent.Parent == game.Workspace.CurrentUnits then
				
				local Unit = RaycastResult.Instance.Parent
				
				if CurrentUpgradeUnit ~= Unit then
					CurrentSelected = true
					CurrentUpgradeUnit = Unit
					CurrentID = Unit:GetAttribute("ID")
					UnitNameLabel.Text = Unit.Name

					local Goal = {} Goal.Position = UDim2.fromScale(0.98, 0.5)
					local Info = TweenInfo.new(0.25, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
					local Tween = TweenService:Create(UnitFrame, Info, Goal)
					Tween:Play()

					if CurrentUpgradeUnit:GetAttribute("Level") ~= CurrentUpgradeUnit:GetAttribute("MaxLevel") then
						UpgradeButton.Visible = true

						local Upgrading = false
						local Connection1 = nil
						Connection1 = UpgradeButton.Activated:Connect(function()
							if CurrentSelected == true then
								if CurrentUpgradeUnit:GetAttribute("ID") == CurrentID  and CurrentUpgradeUnit:GetAttribute("Owner") == Player.Name and Upgrading == false then
									local UpgradeLevel = CurrentUpgradeUnit:GetAttribute("Level") + 1	
									if Player.leaderstats.Cash.Value > ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(CurrentUpgradeUnit.Name):GetAttribute("Cost") then
										Upgrading = true
										UpgradeEvent:FireServer(CurrentUpgradeUnit, CurrentID)

										local Connection2 = nil
										Connection2 = UpgradeEvent.OnClientEvent:Connect(function(CurrentUnit)
											Connection2:Disconnect()
											Upgrading = false
											CurrentUpgradeUnit = CurrentUnit
											print(CurrentUnit:GetAttribute("Level"), CurrentUpgradeUnit:GetAttribute("MaxLevel"))
											if CurrentUnit:GetAttribute("Level") == CurrentUpgradeUnit:GetAttribute("MaxLevel") then
												UpgradeButton.Visible = false
											else
												UpgradeButton.Visible = true
											end

											repeat
												task.wait()
											until CurrentSelected == false
											Connection1:Disconnect()
										end)
									end
								end
							end
						end)
					end
				end
			else
				local Goal = {} Goal.Position = UDim2.fromScale(1.28, 0.5)
				local Info = TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
				local Tween = TweenService:Create(UnitFrame, Info, Goal)
				Tween:Play()
				CurrentSelected = false
				CurrentUpgradeUnit = nil
			end
		end
	end
end)

the serverscript

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local System = require(ServerScriptService.SystemModule)

-- Remote Event
local DeployEvent = ReplicatedStorage.DeployEvent
local UpgradeEvent = ReplicatedStorage.UpgradeEvent

local function EnemyKilled(Enemy)
	wait(0.5)
	Enemy:Destroy()
end

DeployEvent.OnServerEvent:Connect(function(Player, UnitName, UnitPosition, UnitLowestPosition, ID)
	local Unit = ReplicatedStorage.Units:FindFirstChild(0):FindFirstChild(UnitName)
	
	if Player.leaderstats.Cash.Value >= Unit:GetAttribute("Cost") then
		Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Unit:GetAttribute("Cost")
		
		Unit = Unit:Clone()
		Unit.Parent = game.Workspace.CurrentUnits
		Unit:SetPrimaryPartCFrame(UnitPosition)
		Unit:SetAttribute("Owner", Player.Name)
		Unit:SetAttribute("ID", ID)

		local Space = ReplicatedStorage.Assets.Space:Clone()
		Space.Parent = game.Workspace.CurrentAssets
		Space.Position = UnitLowestPosition
		Space.Size = Vector3.new(0.001, Unit:GetAttribute("Space") * 2, Unit:GetAttribute("Space") * 2)
		Space.Anchored = true

		for Quantity, Object in pairs(Unit:GetDescendants()) do
			if Object:IsA("BasePart") then
				Object.Anchored = true
				Object.CollisionGroup = "Units"
			end
		end

		UpgradeEvent.OnServerEvent:Connect(function(Player, UpgradeUnit, CurrentID)
			task.wait()
			
			if UpgradeUnit then
				if Unit:GetAttribute("ID") == CurrentID and UpgradeUnit:GetAttribute("Owner") == Player.Name then
					local UpgradeLevel = Unit:GetAttribute("Level") + 1
					local UpgradeUnit = ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(UnitName)
					if UpgradeUnit and Player.leaderstats.Cash.Value >= UpgradeUnit:GetAttribute("Cost")and UpgradeUnit:FindFirstChild("Humanoid").Health > 0 then
						Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - UpgradeUnit:GetAttribute("Cost")

						Unit:Destroy()
						Unit = ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(UnitName):Clone()
						Unit.Parent = game.Workspace.CurrentUnits
						Unit:SetPrimaryPartCFrame(UnitPosition)
						Unit:SetAttribute("Owner", Player.Name)
						Unit:SetAttribute("ID", CurrentID)

						for Quantity, Object in pairs(Unit:GetDescendants()) do
							if Object:IsA("BasePart") then
								Object.Anchored = true
								Object.CollisionGroup = "Units"
							end
						end

						UpgradeEvent:FireClient(Player, Unit)
					end
				end
			end
		end)
		
		while Unit and Unit.Parent do
			if Unit:GetAttribute("AttackType") == "Shooting" then
				local TargetedEnemy = System.Detect(Unit, Unit:GetAttribute("TargetType"))
				if TargetedEnemy then
					local EnemyHumanoid = TargetedEnemy:FindFirstChild("Humanoid")
					EnemyHumanoid.Health = EnemyHumanoid.Health - Unit:GetAttribute("Damage")

					local Goal = {} Goal.CFrame = CFrame.new(Unit.PrimaryPart.Position, Vector3.new(TargetedEnemy.PrimaryPart.Position.X, Unit.PrimaryPart.Position.Y, TargetedEnemy.PrimaryPart.Position.Z))
					local Info = TweenInfo.new(0.25, Enum.EasingStyle.Exponential)
					local Tween = TweenService:Create(Unit.PrimaryPart, Info, Goal)

					for Quantity, Object in pairs(Unit:GetDescendants()) do
						if Object:IsA("BasePart") then
							Object.Anchored = false
						end
					end

					Tween:Play()
					Tween.Completed:Wait()

					for Quantity, Object in pairs(Unit:GetDescendants()) do
						if Object:IsA("BasePart") then
							Object.Anchored = true
						end
					end

					if EnemyHumanoid.Health <= 0 then
						EnemyKilled(TargetedEnemy)
					end

					task.wait(Unit:GetAttribute("Cooldown") - 0.25)
				end
			end
			
			task.wait()
		end
		
		Space:Destroy()
	end
end)
2 Likes

Can you put the output here?

There should be a line telling X --“ScriptName”
something like that

also You can try :WaitForChild() too i think.

20:13:20.978 WorkableIsaacho01 committed a new version of script ServerScriptService.UnitHandler - Studio
20:13:21.169 td gaem @ 27 Dec 2023 20:13 auto-recovery file was created - Studio
20:13:22.759 250 - Server - UnitData:150
20:13:23.690 Loading Animation Mirrorer… - Server
20:13:24.104 Finished Loading Animation Mirrorer - Server
20:13:24.863 Loading Animation Mirrorer… - Client
20:13:24.918 Finished Loading Animation Mirrorer - Client
20:13:29.514 1 5 - Client - UnitController:303
20:13:29.714 2 5 - Client - UnitController:303
20:13:29.913 3 5 - Client - UnitController:303
20:13:31.829 1 5 - Client - UnitController:303
20:13:32.096 2 5 - Client - UnitController:303
20:13:32.263 3 5 - Client - UnitController:303
20:13:32.413 4 5 - Client - UnitController:303
20:13:32.581 5 5 - Client - UnitController:303
20:13:42.964 :arrow_forward: 1 2 (x9) - Client - UnitController:303
20:13:47.015 2 2 - Client - UnitController:303
20:13:49.430 1 2 - Client - UnitController:303
20:13:50.014 2 2 - Client - UnitController:303
20:13:53.098 1 2 - Client - UnitController:303
20:13:53.165 2 2 - Client - UnitController:303
20:13:55.698 1 2 - Client - UnitController:303
20:13:55.764 2 2 - Client - UnitController:303
20:13:58.013 1 2 - Client - UnitController:303
20:13:58.080 2 2 - Client - UnitController:303
20:14:06.181 :arrow_forward: 1 2 (x2) - Client - UnitController:303
20:14:19.178 :arrow_forward: 1 5 (x4) - Client - UnitController:303
20:14:19.345 :arrow_forward: 3 5 (x2) - Client - UnitController:303
20:14:27.781 ServerScriptService.UnitHandler:49: attempt to index nil with ‘FindFirstChild’ - Server - UnitHandler:49
20:14:27.781 Stack Begin - Studio
20:14:27.781 Script ‘ServerScriptService.UnitHandler’, Line 49 - Studio - UnitHandler:49
20:14:27.782 Stack End - Studio
20:14:27.813 :arrow_forward: 5 5 (x4) - Client - UnitController:303
20:16:02.716 :arrow_forward: td gaem auto-recovery file was created (x9) - Studio

I think we need the full code of UnitHandler as i can’t see any ‘FindFirstChild’ in any of the codes at 49th

this is the whole code for the severscript one


But there is no ‘FindFirstChild’ at 49?

Do you perhaps have space in the top
if thats the case, it will be 48 for me

oh wait sorry yes theres spaces skipped 2 lines


if it gives error for UpgradeUnit:FindFirstChild("Humanoid")
I think, “UpgradeUnit” doesn’t seem to have any “Humanoid” in them
is it a Dummy type thing? (I mean NPC, R6, R15 anything) or just a value

yes its a character Upgrade unit is a r15 character

Can you use

Print(UpgradeUnit)
Print(UpgradeUnit.Parent)
Print(UpgradeUnit.Name)

Just to see if it grabs it right
1st Checks Target’s Name
2nd Checks its Parent to if its your TargetNPC or Humanoiditself
3rd Check the name again to be sure xd (works same as 1st but why not)

can you then put the output here

oh you should also try :WaitForChild() instead of :FindFirstChild()
The code might be working faster than it loads

uh this is so weird, is it not getting the target at all?

Try :WaitForChild() instead of :FindFirstChild() to them to see if it works --WithoutPrints

its not like its always getting an error it just randomly pops up, especially when if unit upgrades(not max yet), another diffrent unit(not same type) upgrades and returns the the first one it gets this error

it shouldn’t error, can you check the unit from the folders to see, after upgrade any changes happen to it?


with print ^^^^

image

without print ^^^

there is nothing wrong with the attributes everything is assigned correctly its somehow not getting it for some reason

Do you have team create?
image

if you want you can add me

I can’t seem to understand why its printing like that as its supposed to print something else

image
You seem to destroy it after upgrade i think, because it becomes nil after upgrade