When holding item it moves out of view

I tried to make a script for dragging items, but it not working properly. When I hold item it moves out of player’s view. I wanted to make dragging with a limit (player can’t hold item at far distance).

I tried to make that via raycast but the issue is the same. I also searched on creator hub but didn’t found any solution.

Sometime when I tested to hold items, it worked fine but in another test it was broken again even when I didn’t changed anything in the script.

Here’s the part of the code:

local runService = game:GetService('RunService')
local players = game:GetService('Players')

local plr = players.LocalPlayer
local mouse = plr:GetMouse()
local camera = workspace.CurrentCamera
local state = plr:WaitForChild('CurrentState')::StringValue

local target:Model

local dist = script:WaitForChild('Dist')

runService.RenderStepped:Connect(function(dt)
	if string.find(state.Value, 'Holding') and target then
		local rotOffset = CFrame.Angles(math.rad(target:GetAttribute('RotationOffset').X), math.rad(target:GetAttribute('RotationOffset').Y), math.rad(target:GetAttribute('RotationOffset').Z))
		
		local d = math.min((camera.CFrame.Position - mouse.Hit.Position).Magnitude - 1, dist.Value)
		
		local cf = (camera.CFrame.Position - mouse.Hit.Position).Unit * d
		
		target:SetPrimaryPartCFrame(CFrame.new(camera.CFrame.Position - cf) * rotOffset)
	end
end)

and place itself:
place.rbxl (110.1 KB)

2 Likes

Have you tried using raycast blacklist? It might be because raycast hits your character.

2 Likes

Yes, I tried adding only map to whitelist, but it didn’t work. And also there’s only a camera part in the world without character

Did you add camera part into the blacklist? Because camera part can also be detected or you can disable the camera part’s canquery property

I tried all that but nothing works. Also, when the player is holding an item, its CanQuerry property is disabled

Try adding this line camera.CameraType = Enum.CameraType.Scriptable inside the main camera script. Also, when I looked into the code, I don’t think it’s disabling the CanQuery property (it only enables it from what I know).

1 Like

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