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

Why isnt my code detecting the closest model?

Asked by 4 years ago
Edited 4 years ago

I have a structure that is placed on top of a resource. all of the resource spots are models placed in a folder in the workspace. i look for the closest resource to the placed building to determine which resource the structure is placed on. For some reason though, this code doesnt find the closest resource, and doesnt seem to take location into account whatsoever when deciding which is closest. (returns same resource model no matter where it was placed)

I have heard something about how sometimes cloning unions, meshes, and maybe spheres can corrupt the object's physical properties. i sincerely hope this is not the case.

If there is something wrong with the code i am using, please feel free to reply the problem. any and all input is appreciated.



local function FindClosestCloud(part) local items = game.Workspace.Resources:GetChildren() local Closest local Distance = math.huge for i,item in pairs(items) do local newDistance = (item.PrimaryPart.Position - part.Position).magnitude if newDistance < Distance then Closest = item Distance = newDistance print(item) end end return Closest end script.Parent.ColorMe.Event:Connect(function() local cloud = FindClosestCloud(script.Parent.PrimaryPart) print(cloud) local team = script.Parent.Team.Value local alplr = game.Players:GetChildren() local plr for i, p in pairs(alplr) do if p.Team == team then plr = p end end if cloud then local Resource = cloud.Identity.Value while true do wait(1) if plr.Inventory:FindFirstChild(Resource) then plr.Inventory:FindFirstChild(Resource).Value = plr.Inventory:FindFirstChild(Resource).Value + 5 end end end end)

Image of the hierarchy (sorry for my bad handwriting in the image, i dont believe the snipping tool has a text box option)

If you do not see anything wrong with the script, hierarchy, or otherwise, please feel free to say so as well

0
We cannot view your image... Also do data tsuff from the server greatneil80 2647 — 4y
0
@greatneil80 data stuff? Dudeguy90539 36 — 4y
0
i am unsure how you cant see it Dudeguy90539 36 — 4y

Answer this question