Holding object wont center on mouse

I am trying to make a script that can hold food objects, throw them, and drop them but holding the object wont center in the middle of the mouse. The object is downwards and I have tried multiple things to fix this but nothing has worked. The script is suppose to move around the player and not be able to move it in or out in third person. Here is the local script:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local mouse = player:GetMouse()
local event = ReplicatedStorage:WaitForChild("HoldItemEvent")
local camera = workspace.CurrentCamera

local holding = false
local heldPart = nil

local minDistance = 5
local maxDistance = 15
local distance = 5
local targetDistance = distance

mouse.Button1Down:Connect(function()
	if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then return end
	local target = mouse.Target
	if not target then return end
	local model = target:FindFirstAncestor("Bananas")
	if model and model:FindFirstChildWhichIsA("ClickDetector") then
		if not holding then
			local modelCenter = model:GetBoundingBox().Position
			local charPos = player.Character.HumanoidRootPart.Position
			local distFromCharToModel = (charPos - modelCenter).Magnitude
			targetDistance = math.clamp(distFromCharToModel, minDistance, maxDistance)
			distance = targetDistance
			event:FireServer("pickup", {position = mouse.Hit.Position})
			holding = true
		end
	elseif holding then
		event:FireServer("drop")
		holding = false
		targetDistance = 5
		heldPart = nil
	end
end)

event.OnClientEvent:Connect(function(action, data)
	if action == "heldModel" then
		heldPart = data
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed or not holding then return end
	if input.KeyCode == Enum.KeyCode.T then
		local direction = camera.CFrame.LookVector
		local charPos = player.Character.HumanoidRootPart.Position
		local throwPosition = charPos + direction * targetDistance
		event:FireServer("throw", {pos = throwPosition, dir = direction})
		holding = false
		targetDistance = 5
		heldPart = nil
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if not holding then return end
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		targetDistance = math.clamp(targetDistance + input.Position.Z, minDistance, maxDistance)
	end
end)

RunService.RenderStepped:Connect(function()
	if holding and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		distance = distance + (targetDistance - distance) * 0.25

		local root = player.Character.HumanoidRootPart
		local ray = mouse.UnitRay

		local rayParams = RaycastParams.new()
		rayParams.FilterDescendantsInstances = {player.Character}
		if heldPart then
			table.insert(rayParams.FilterDescendantsInstances, heldPart)
		end
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist

		local origin = root.Position
		local direction = ray.Direction.Unit

		local raycast = workspace:Raycast(origin, direction * distance, rayParams)
		local finalPos = origin + direction * distance
		if raycast then
			finalPos = raycast.Position
		end

		local lookAt = CFrame.lookAt(finalPos, camera.CFrame.Position)

		event:FireServer("update", {
			position = finalPos,
			rotationCFrame = lookAt
		})
	end
end)

Still waiting on how to fix this. The issue is inside RenderStepped

Have you tried using drag detectors?

documentation: 3D drag detectors | Documentation - Roblox Creator Hub

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