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

I'm coding rn and for some reason Position is not a valid member of Model anyone know how to fix?

Script has changed since I've made progress but it is still showing the same error.

local tower = script.Parent local mobs = workspace.Mobs

local function FindNearestTarget() local maxDistance = 50 local nearestTarget = nil

for i, target in ipairs(mobs:GetChildren()) do
	local distance = (target.HumanoidRootPart.Position - tower.Position).Magnitude
	print(target.Name, distance)
	if distance < maxDistance then
		print(target.Name, "is the nearest target found so far")
		nearestTarget = target
		maxDistance = distance
	end 
end

return nearestTarget

end

while true do local target = FindNearestTarget() if target then target.Humanoid:TakeDamage(25) end

task.wait(1)

end

does anyone know how to fix it?

2 answers

EDIT

for i, target in ipairs(mobs:GetChildren()) do
	if target ~= nil then
		if target:FindFirstChildOfClass("Humanoid") then
			local distance = (target.HumanoidRootPart.Position - 	tower.PrimaryPart.Position).Magnitude)

    print(target.Name, distance)
    if distance < maxDistance then
        print(target.Name, "is the nearest target found so far")
        nearestTarget = target
        maxDistance = distance
    end 
end

return nearestTarget

32 comments

0

ok so I updated my script but is still showing the same error, do I need to do the same thing you said or is there a different way. Also it shows that Position is in the code and i'm not able to turn it into a primary part do you now why?

0

Yes you should do what i said.

0

If theres no primary part, the script will not work.

0

ok I will, put what should I change the script to.

0

also for some reason when I try to put the primary part into a part it cancels it right after I clicked it onto a part do you know how to fix it or any solutions?

0

Strange.. try setting the primary part via script

0

ok put where should I put the script since the changed script you send is no longer in the script

0

Oh no! keep the code in the script, just add the line where you set the primary part

0

ok I added back the code but now it's saying Workspace.Stone Tower.TowerTesting:4: attempt to index nil with 'Position' (to clarify Stone Tower is my tower that is gonna be used to attack the enimes and TowerTesting is the script name. Do you know how to fix this?

0

tower.PrimaryPart = tower:WaitForChild(PartName)

0

where do I put this line of code, because it's still showing the same error.

0

any

0

The error means that the code is trying to index something that does not exist.

0

ok I put it on line 4, and it shows some red text underneath some code, does that mean it doesn't work?

0

Can you send me the code?

0

I posted it in answers.

0

Does the tower have a humanoid?

0

no it doesn't.

0

Okay, make an invisible part inside of the tower, name it to "PrimaryPart" and change line 2 to local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude

0

ok I did and it says Workspace.Stone Tower.TowerTesting:4: attempt to index nil with "HumanoidRootPart' and the error is on line 2

0

also here is the youtube link for the tutorial I was on, maybe you can get some info to help me https://youtu.be/xSkSGO8rXUM

0

I've edited my comment, try that

0

do I delete the other stuff that isn't in your code?

0

Nah, you

0

You'll replace the code inside of the FindNearestTarget with mine

0

ok I updated my script so tell me if it's supposed to look like yours + some of mine.

0

How far have you made in the tutorial?

0

I've made it to like the beginning of trying to find the nearest target.

0

also it is showing this error but now I think we are getting closer and closer to fixing it! Workspace.Stone Tower.TowerTesting:26: Expected 'end' (to close 'then' at line 14), got ; did you forget to close 'then' at line 19? - Studio - TowerTesting:26

0

I'm going to go to sleep now so we can figure out the problem tomorrow, ok bye!

0

You should just redo the tutorial you know.

0

ok so i fixed the problem and now im on part 6 of the tutorial and im kind of stuck.

Is my script supposed to look like this?

local tower = script.Parent local mobs = workspace.Mobs

local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude tower.PrimaryPart = tower:WaitForChild(PartName) print(distance)

local function FindNearestTarget() local maxDistance = 50 local nearestTarget = nil

for i, target in ipairs(mobs:GetChildren()) do
	if target ~= nil then
		if target:FindFirstChildOfClass("Humanoid") then
			local distance = (target.HumanoidRootPart.Position - tower.PrimaryPart.Position).Magnitude

			print(target.Name, distance)

if distance < maxDistance then print(target.Name, "is the nearest target found so far") nearestTarget = target maxDistance = distance end end

return nearestTarget

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