Issue with mouse dragging objects to the void

Basically I have a mouse dragging script that moves certain objects to your mouses position when you drag them but theres an issue where if you drag it fast enough you can move it anywhere(the sky,water,void,etc) but theres supposed to be a max distance that you can drag the object to which isn’t working.

(for reference, “NoZone” is a model with lots of invisible walls placed inside of it that surround the map)

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local MoveEvent = ReplicatedStorage.Events.MoveRe

local function movePart(player, action,part, pos, ori)
	
	if action == "Move" then
		if part.Pickable then
			part.Position = pos
			part.Orientation = ori
		end
	end
	
	if action == "Disrupt" then
		if part.Pickable then
			--part.Position = pos
			print("Canceled Grab Action")
		end
	end
	
	
	
end

MoveEvent.OnServerEvent:Connect(movePart)

Code(localscript):

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local target = nil
local t0ri = nil
local down = false 
local runServ = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

local MoveEvent = Events.MoveRe

local function getTarget()
	if mouse.Target then
		local moveable = mouse.Target:FindFirstChild("Pickable")
		if moveable and moveable.value then
			target = mouse.Target
			t0ri = target.Orientation
			mouse.TargetFilter = target
			down = true
		end
	end
end

local function moveTarget()
	if down and target then
		if mouse.Hit.Position ~= game.Workspace["NoZone"]:GetChildren().Position then
			
		local distanceFromItem = player:DistanceFromCharacter(target.Position)
		if distanceFromItem < 20 then
			for i,v in pairs(target:GetTouchingParts()) do
				if v.Name ~= "NoZone" or v.Name ~= "Water" then
					


					local posX,posY,posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
					local pos = Vector3.new(posX,posY + (target.Size.Y/2),posZ)
					MoveEvent:FireServer("Move",target, pos, t0ri)
				elseif v.Name == "NoZone" or v.Name == "Water" then
					
					local PlayerPos = player.Character.HumanoidRootPart.CFrame
					MoveEvent:FireServer("Disrupt", target, PlayerPos)
					
				end
				end
				end
			
		end
		end
end

local function finishMove()
	down = false 
	mouse.TargetFilter = nil
	target = nil
	t0ri = nil
	
end

mouse.Button1Down:Connect(getTarget)
runServ.RenderStepped:Connect(moveTarget)
mouse.Button1Up:Connect(finishMove)

Gif of what its supposed to do and maintain: https://gyazo.com/9e25d01d2589b90f1d24fa2225e6e461

Gif of the issue: https://gyazo.com/1453ee3825e82399a6b02b62857c40da

Make sure to check the distance of the position the player is trying to move the item to.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local target = nil
local t0ri = nil
local down = false 
local runServ = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

local MoveEvent = Events.MoveRe

local function getTarget()
	if mouse.Target then
		local moveable = mouse.Target:FindFirstChild("Pickable")
		if moveable and moveable.value then
			target = mouse.Target
			t0ri = target.Orientation
			mouse.TargetFilter = target
			down = true
		end
	end
end

local function moveTarget()
	if down and target then
		if mouse.Hit.Position ~= game.Workspace["NoZone"]:GetChildren().Position then
			local distanceFromItem = player:DistanceFromCharacter(target.Position)
			if distanceFromItem < 20 then
				for i,v in pairs(target:GetTouchingParts()) do
					if v.Name ~= "NoZone" or v.Name ~= "Water" then
						local posX,posY,posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
						local pos = Vector3.new(posX,posY + (target.Size.Y/2),posZ)
						if player:GetDistanceFromCharacter(pos) < 20 then
							MoveEvent:FireServer("Move",target, pos, t0ri)
						end
					elseif v.Name == "NoZone" or v.Name == "Water" then
						local PlayerPos = player.Character.HumanoidRootPart.CFrame
						MoveEvent:FireServer("Disrupt", target, PlayerPos)
					end
				end
			end
		end
	end
end

local function finishMove()
	down = false 
	mouse.TargetFilter = nil
	target = nil
	t0ri = nil
	
end

mouse.Button1Down:Connect(getTarget)
runServ.RenderStepped:Connect(moveTarget)
mouse.Button1Up:Connect(finishMove)
1 Like

This is the output your piece of code gives me for some reason. (It was a typo, fixed and it works, thanks!)