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)