Model Pickup and Placement System

Hello,
I need some help to make this building system [Timeline: 0:08] [Vidéo]

I know how to teleport the part but not how to limit the Magnetude without remove all collisions.

Here is my code,

local MyPlayer = game.Players.LocalPlayer
local Camera = game.Workspace.Camera
local Mouse = MyPlayer:GetMouse()
local PhysicsServ = game:GetService(“PhysicsService”)
local PhysGName = “OriginePoint”
local UserInputService = game:GetService(“UserInputService”)
local TweenService = game:GetService(“TweenService”)

function CreateCursorPoint()
if game.Workspace:FindFirstChild(MyPlayer.UserId…“CursorPoint”) == nil then
print(“Creating Point”)
local CursorPoint = Instance.new(“Part”)
CursorPoint.Name = MyPlayer.UserId…“CursorPoint”
CursorPoint.Parent = game.Workspace
CursorPoint.Anchored = true
CursorPoint.CastShadow = false
CursorPoint.Transparency = 0.25
CursorPoint.Position = Vector3.new(0,0,0)
end
end
CreateCursorPoint()

local GetCursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId…“CursorPoint”)
local CPointMoveSpeed = TweenInfo.new(0.1)

function TeleportCursorPoint()
while game:GetService(“RunService”).Heartbeat:Wait() do
local FinalPos = Camera.CFrame.Position + (Mouse.Hit.Position - game.Workspace[MyPlayer.Name].Head.CFrame.Position)
print(FinalPos)
print(FinalPos.Unit * 8)
local MouseTween = TweenService:Create(GetCursorPoint, CPointMoveSpeed, { Position = FinalPos })
Mouse.TargetFilter = GetCursorPoint
MouseTween:Play()
–GetCursorPoint.Position = FinalPos
end

end

local CursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId…“CursorPoint”)
PhysicsServ:SetPartCollisionGroup(CursorPoint, PhysGName)

local CursorPointCoroutine = coroutine.create(TeleportCursorPoint)
coroutine.resume(CursorPointCoroutine)

After 10 hours of thinking i really need help.

  • FoxyFoxy67

So you mean it’s placing the center of your Part at the mouse.hit Position?
If so you need to calculate half the total values for X,Y, and Z sizes and make that your FinalPos if I read your code correctly.
It makes it tough when you don’t indent your code. You can copy your code and paste it here then use the tool above that looks like </>, or just place 3 backticks (```) before and after your chunk of code.

Try searching for other posts that show how to calculate where your Model needs be place with mouse movement.

Yes i want to center my part at the mouse.hit Position but with a limited radius around my player

local MyPlayer = game.Players.LocalPlayer
local Camera = game.Workspace.Camera
local Mouse = MyPlayer:GetMouse()
local PhysicsServ = game:GetService("PhysicsService")
local PhysGName = "OriginePoint"
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

function CreateCursorPoint()
	if game.Workspace:FindFirstChild(MyPlayer.UserId.."CursorPoint") == nil then
		print("Creating Point")
		local CursorPoint = Instance.new("Part")
		CursorPoint.Name = MyPlayer.UserId.."CursorPoint"
		CursorPoint.Parent = game.Workspace
		CursorPoint.Anchored = true
		CursorPoint.CastShadow = false
		CursorPoint.Transparency = 0.25
		CursorPoint.Position = Vector3.new(0,0,0)
	end
end
CreateCursorPoint()

local GetCursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId.."CursorPoint")
local CPointMoveSpeed = TweenInfo.new(0.1)

function TeleportCursorPoint()
	while game:GetService("RunService").Heartbeat:Wait() do
		local FinalPos = Camera.CFrame.Position + (Mouse.Hit.Position - game.Workspace[MyPlayer.Name].Head.CFrame.Position)
		print(FinalPos)
		print(FinalPos.Unit * 8)
		local MouseTween = TweenService:Create(GetCursorPoint, CPointMoveSpeed, { Position = FinalPos })
		Mouse.TargetFilter = GetCursorPoint
		MouseTween:Play()
		--GetCursorPoint.Position = FinalPos
	end
	
end

local CursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId.."CursorPoint")
PhysicsServ:SetPartCollisionGroup(CursorPoint, PhysGName)


local CursorPointCoroutine = coroutine.create(TeleportCursorPoint)
coroutine.resume(CursorPointCoroutine)

Try using math.clamp when setting up your FinalPos using Mouse.Hit.Position.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()

local connection = nil
local target = nil

mouse.Button1Down:Connect(function()
	if mouse.Target then
		if mouse.Target:IsA("BasePart") then
			target = mouse.Target
			target.Anchored = true
			target.CanCollide = false
			local distance = (target.Position - hrp.Position).Magnitude
			if distance > 30 then
				return
			end
			mouse.TargetFilter = target
			connection = mouse.Move:Connect(function()
				target.Position = Vector3.new(mouse.Hit.Position.X, mouse.Hit.Position.Y + target.Size.Y/2, mouse.Hit.Position.Z)
			end)
		end
	end
end)

mouse.Button1Up:Connect(function()
	if not target then
		return
	end
	
	local distance = (target.Position - hrp.Position).Magnitude
	if distance > 30 then
		return
	end
	
	target.CanCollide = true
	
	if connection then
		connection:Disconnect()
		connection = nil
		mouse.TargetFilter = nil
	end
end)

Here’s a sample script I just created which lets you move BasePart instances with the mouse. You start by holding the left mouse button while the cursor is hovered a BasePart, as long as your player’s character model is within 30 studs of the BasePart if you move your mouse the BasePart will be dragged following the mouse’s cursor, once the left mouse button is released, if the mouse’s position is within 30 studs of the player’s character model then the BasePart is placed at the mouse’s location, otherwise the BasePart is not placed. This arbitrary distance of “30” can of course be changed & the script can also be changed to accommodate the movement of other instance types, i.e; models etc.

And here’s the same script which implements tweens to handle the movement of the dragged BasePart so that its movement appears more fluid.

local tweens = game:GetService("TweenService")

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()

local connection = nil
local target = nil

mouse.Button1Down:Connect(function()
	if mouse.Target then
		if mouse.Target:IsA("BasePart") then
			target = mouse.Target
			target.Anchored = true
			target.CanCollide = false
			local distance = (target.Position - hrp.Position).Magnitude
			if distance > 30 then
				return
			end
			mouse.TargetFilter = target
			connection = mouse.Move:Connect(function()
				local tween = tweens:Create(target, TweenInfo.new(0.1), {Position = Vector3.new(mouse.Hit.Position.X, mouse.Hit.Position.Y + target.Size.Y/2, mouse.Hit.Position.Z)})
				tween:Play()
			end)
		end
	end
end)

mouse.Button1Up:Connect(function()
	if not target then
		return
	end
	
	local distance = (target.Position - hrp.Position).Magnitude
	if distance > 30 then
		return
	end
	
	target.CanCollide = true
	
	if connection then
		connection:Disconnect()
		connection = nil
		mouse.TargetFilter = nil
	end
end)
1 Like

I didn’t think about it, but yes, i go do some math’s to implement it

Try @Forummer script first. His doesn’t run the movement part if the distance is > 30 so it’s a bit cleaner than my way of doing it.

Hello,
I tested but is not what i really want
i need to move the part but with a limited distance around of the player camera
like on the video (i go test to implement a math.clamp() and see how is it working )

Edit: With a clamp it make me a square around me, i know how i can do but there is no collisions if i do it like this

Thanks anyway,

There is my 2nd solution, but the part doesn’t have any collision :confused:

local MyPlayer = game.Players.LocalPlayer
local Camera = game.Workspace.Camera
local Mouse = MyPlayer:GetMouse()
local PhysicsServ = game:GetService("PhysicsService")
local PhysGName = "OriginePoint"
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

function CreateCursorPoint()
	if game.Workspace:FindFirstChild(MyPlayer.UserId.."CursorPoint") == nil then
		print("Creating Point")
		local CursorPoint = Instance.new("Part")
		CursorPoint.Name = MyPlayer.UserId.."CursorPoint"
		CursorPoint.Parent = game.Workspace
		CursorPoint.Anchored = true
		CursorPoint.CastShadow = false
		CursorPoint.Transparency = 0.25
		CursorPoint.Position = Vector3.new(0,0,0)
	end
end
CreateCursorPoint()

local GetCursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId.."CursorPoint")
local CPointMoveSpeed = TweenInfo.new(0.1)

function TeleportCursorPoint()
	while game:GetService("RunService").Heartbeat:Wait() do
		local FinalPos = Camera.CFrame.Position + (Mouse.Hit.Position - game.Workspace[MyPlayer.Name].Head.CFrame.Position).Unit *8 -- I added the .Unit
		print(FinalPos)
		print(FinalPos.Unit * 8)
		
		local MouseTween = TweenService:Create(GetCursorPoint, CPointMoveSpeed, { Position = FinalPos })
		Mouse.TargetFilter = GetCursorPoint
		MouseTween:Play()
		--GetCursorPoint.Position = FinalPos
	end
	
end

local CursorPoint = game.Workspace:WaitForChild(MyPlayer.UserId.."CursorPoint")
PhysicsServ:SetPartCollisionGroup(CursorPoint, PhysGName)


local CursorPointCoroutine = coroutine.create(TeleportCursorPoint)
coroutine.resume(CursorPointCoroutine)

Edit :
I added the .Unit * 8 after the FinalPos value

It’s an example from which you should take inspiration from.

Yeah, i already see that system and understanded how i can do it, but now i just have a collision problem.

Part.CanCollide = true

I have enabled the collisions, but it doesn’t work

Someone have any solutions to solve the collision problem ?

Edit : i Added a video of the issue
robloxapp-20220203-2331345.wmv (1.1 MB)

Hello
I close this, i found my solution with a friend.

The Solution:
You set a maximum distance of studs, for example : “8”
local FinalPos = Camera.CFrame.Position + (Mouse.Hit.Position - Camera.CFrame.Position)

i check
if the (Mouse.Hit.Position - Camera.CFrame.Position).Magnitude is superior to 8 then i do

FinalPos = Camera.CFrame.Position + (Mouse.Hit.Position - Camera.CFrame.Position).Unit * 8
else i set
FinalPos to (Camera.CFrame.Position + (Mouse.Hit.Position - Camera.CFrame.Position)

and i set Mouse.target.Position to FinalPos

You can add a tween for smooth.
Don’t Forget to put the code in a loop.

Thanks for help,

  • FoxyFoxy67
1 Like

This isn’t a solution though, it’s just telling readers that it has been solved.
If anyone has this problem and finds this post it won’t help them.

Please mention what you did that solved the problem you were having.

oh, yes your right, wait a couple of minute’s i go modify my message.