Help, with mouse tracking system

i need help with a script, that runs localy, which creates a part, and updates the position of the part to the mouse everytime the person equips the tool, only problem, is that once i stop moving the mouse, the part magicly moves towards the camera for no apparent reason, i have been struggling to find the answer to the problem

local tool = script.Parent
local cooldown = false
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = script:WaitForChild("Attack")
local animater = humanoid:LoadAnimation(animation)
local mouse = player:GetMouse()
local toolequipped = false

-- Create the tracking part
local trackingPart = Instance.new("Part")
trackingPart.Size = Vector3.new(1, 1, 1)
trackingPart.Color = Color3.new(0.215686, 1, 0)
trackingPart.Transparency = 0.5
trackingPart.Shape = Enum.PartType.Ball
trackingPart.Anchored = true
trackingPart.CanCollide = false
trackingPart.Parent = workspace

tool.Equipped:Connect(function()
	toolequipped = true
	while toolequipped do
		local hit = mouse.Hit
		if hit then
			local characterPosition = character.PrimaryPart.Position
			local hitPosition = hit.Position
			local distance = (hitPosition - characterPosition).magnitude
			local minDistance = 5

			if distance > minDistance then
				trackingPart.Position = hitPosition + Vector3.new(0, trackingPart.Size.Y / 2, 0)
			else
				local direction = (hitPosition - characterPosition).unit
				trackingPart.Position = characterPosition + direction * minDistance + Vector3.new(0, trackingPart.Size.Y / 2, 0)
			end
		end

		wait(0.1)
	end
end)

tool.Unequipped:Connect(function()
	toolequipped = false
	trackingPart.Position = Vector3.new(0, -1000, 0)
end)

tool.Activated:Connect(function()
	if not cooldown and toolequipped then
		cooldown = true
		animater:Play()
		game.ReplicatedStorage.RemoteTools.BoneThrow:FireServer(player)
		wait(9)
		cooldown = false
	end
end)
1 Like

i also tryed to ask the roblox scripting assistent ai for help, no help at all

After this line, add

mouse.TargetFilter = trackingPart
2 Likes

DUDE YOUR A LIVE SAVER!!!, THANKS SO Much!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.