So I'm making a tool that points towards the nearest piece of scrap metal, it finds the nearest scrap but when rotating the pointer it will also rotate the player and cause them to fly everywhere please help, I have no idea how to fix this
Heres my code:
--Define Variables local CAS = game:GetService("ContextActionService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() local Tool = script.Parent local character if not player.Character then print("waiting for character") player.CharacterAdded:wait(); character = player.Character else character = player.Character end local hum = character:WaitForChild("HumanoidRootPart") local equipped = false local Model = workspace.Scrap local Core = hum local closest local Pointer = Tool.Point function GetDistanceFromChildren() local Position_Contain = {} for Index,Child in pairs(Model:GetChildren()) do table.insert(Position_Contain,Index,(Child.Position - Core.Position).Magnitude) end for Index,Child in pairs(Model:GetChildren()) do if (Core.Position - Child.Position).Magnitude == math.min(unpack(Position_Contain)) then return Child end end end function Equip() equipped = true while equipped do closest = GetDistanceFromChildren() print(closest) Pointer.CFrame = CFrame.new(Pointer.Position, closest.Position) wait() end end function Unequip() equipped = false end Tool.Equipped:Connect(Equip) Tool.Unequipped:Connect(Unequip)
Heres whats happening when I equip it: https://www.youtube.com/watch?v=SB6YMqNXqsg&feature=youtu.be
Please help, although its amazing as hell its not what its supposed to be
Maybe your tool is anchored? You need to have it unanchored and welded or else it'll be all glitchy like this. Happens to me sometimes.