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

Why my output makes a error: " Workspace.Paragon.Main:65: attempt to index nil with 'Position' "?

Here is the script:

wait()

local player = game.Players
local rp = game:GetService("ReplicatedStorage")

--model

local Model = script.Parent
local Assets = workspace.GameAssets

local beam = Model.Sentry.Beam

-- animations

local Folder = script.Animations
local Idle = Folder.Idle
local Attack = Folder.Attack

local LoadIdle2 = script.Parent.Humanoid:LoadAnimation(Idle)
local LoadAttack = script.Parent.Humanoid:LoadAnimation(Attack)

-- ps

local pS = game:GetService("PhysicsService")

--unidade
local RootPart = Model.PrimaryPart
local Head = Model:WaitForChild("Head")

-- config
local config = Model:WaitForChild("Configuration")
local Dano = config:WaitForChild("Damage")
local Range = config:WaitForChild("Range")
local Cooldown = config:WaitForChild("Cooldown")

-- inimigos

local Enemies = Assets:WaitForChild("Enemies")
local CloseEnemies = {}

local TargetSelected = nil

LoadIdle2:Play()

local function AttackAlvo()
	
	if CloseEnemies[1] == nil then
		return
	end
	
	if TargetSelected == nil then
		
		-- n tem target, ent vai adicionar um novo
		TargetSelected = CloseEnemies[1]
		
		if TargetSelected ~= nil then
			Model.Shot:Play()
			if TargetSelected and TargetSelected.PrimaryPart then
				coroutine.resume(coroutine.create(function()
					local att = Instance.new("Attachment")
					att.Parent = TargetSelected.PrimaryPart
					beam.Attachment1 = att
					while TargetSelected do
						if TargetSelected then
							local EnemyPos = TargetSelected.PrimaryPart.Position
							local LookPos = Vector3.new(EnemyPos.X, RootPart.Position.Y, EnemyPos.Z)
							RootPart.CFrame = CFrame.new(RootPart.Position, LookPos)
							TargetSelected:FindFirstChild("Humanoid"):TakeDamage(Dano.Value)
							if TargetSelected:FindFirstChild("Humanoid").Health < 1 then
								TargetSelected = nil
								break
							end
							wait(Cooldown.Value)
						end
					end
				end))
			end
		end
		
		if TargetSelected ~= nil then
			if TargetSelected:WaitForChild("Humanoid") then
				
				script.Parent.Shot:Play()
				LoadAttack:Play()
				-- oque o ataque vai fazer aqui

				if TargetSelected.PrimaryPart == nil then
					return
				else
					print("ok")
				end


			end
		end
		
		
	end
	
	LoadIdle2:Play()
	
end




local function ProcurarAlvo()
	
	CloseEnemies = {}
	
	for _, Enemy in pairs(Enemies:GetChildren()) do
		
		if RootPart ~= nil then
			if Enemy.PrimaryPart == nil then
				warn("ratio o bixo n tem primary part")
				return
			end
			
			
			local distance = (RootPart.Position - Enemy.PrimaryPart.Position).magnitude
			
			if (distance < Range.Value) then
				table.insert(CloseEnemies, Enemy)
			end
			
			
		else
			warn("po man esqueceu o rootpart no maluco vei")
		end
		
	end
	
	AttackAlvo()
	
	
end








while true do
	wait(0.1)
	
	ProcurarAlvo()
end

1 answer

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