Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago
Edited 1 year ago

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

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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
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? BonkedWasTaken 0 — 1y
0
Yes you should do what i said. SEAN_YT213 139 — 1y
0
If theres no primary part, the script will not work. SEAN_YT213 139 — 1y
0
ok I will, put what should I change the script to. BonkedWasTaken 0 — 1y
View all comments (28 more)
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? BonkedWasTaken 0 — 1y
0
Strange.. try setting the primary part via script SEAN_YT213 139 — 1y
0
ok put where should I put the script since the changed script you send is no longer in the script BonkedWasTaken 0 — 1y
0
Oh no! keep the code in the script, just add the line where you set the primary part SEAN_YT213 139 — 1y
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? BonkedWasTaken 0 — 1y
0
tower.PrimaryPart = tower:WaitForChild(PartName) SEAN_YT213 139 — 1y
0
where do I put this line of code, because it's still showing the same error. BonkedWasTaken 0 — 1y
0
any SEAN_YT213 139 — 1y
0
The error means that the code is trying to index something that does not exist. SEAN_YT213 139 — 1y
0
ok I put it on line 4, and it shows some red text underneath some code, does that mean it doesn't work? BonkedWasTaken 0 — 1y
0
Can you send me the code? SEAN_YT213 139 — 1y
0
I posted it in answers. BonkedWasTaken 0 — 1y
0
Does the tower have a humanoid? SEAN_YT213 139 — 1y
0
no it doesn't. BonkedWasTaken 0 — 1y
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 SEAN_YT213 139 — 1y
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 BonkedWasTaken 0 — 1y
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 BonkedWasTaken 0 — 1y
0
I've edited my comment, try that SEAN_YT213 139 — 1y
0
do I delete the other stuff that isn't in your code? BonkedWasTaken 0 — 1y
0
Nah, you SEAN_YT213 139 — 1y
0
You'll replace the code inside of the FindNearestTarget with mine SEAN_YT213 139 — 1y
0
ok I updated my script so tell me if it's supposed to look like yours + some of mine. BonkedWasTaken 0 — 1y
0
How far have you made in the tutorial? SEAN_YT213 139 — 1y
0
I've made it to like the beginning of trying to find the nearest target. BonkedWasTaken 0 — 1y
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 <eof>; did you forget to close 'then' at line 19? - Studio - TowerTesting:26 BonkedWasTaken 0 — 1y
0
I'm going to go to sleep now so we can figure out the problem tomorrow, ok bye! BonkedWasTaken 0 — 1y
0
You should just redo the tutorial you know. SEAN_YT213 139 — 1y
0
ok so i fixed the problem and now im on part 6 of the tutorial and im kind of stuck. BonkedWasTaken 0 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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

Answer this question