Issue with my code

Hello! I have been dealing with a problem in my code. I made a placement system but when the player is placing the object it clips through things like walls. Does anyone know how I can fix this? Here is my code:

								if placingobject == true then
									mouse.TargetFilter = previewobject
									if previewobject:FindFirstChild("MainPart") then
										local objectcframe = CFrame.new(math.round(mouse.Hit.Position.X/grid)*grid,math.round(mouse.Hit.Position.Y+previewobject.PrimaryPart.Size.Y/2), math.round(mouse.Hit.Position.Z/grid)*grid)
										local objectangles = CFrame.Angles(0, math.rad(rotationamount), 0)
										previewobject:SetPrimaryPartCFrame(objectcframe*objectangles)
									end
								else
									previewobject:Destroy()
								end
								if placingobject == false then
									script.Parent.Inventorybtn.Visible = true
									script.Parent.Rotation.Visible = false
								end
							end)```

Preview object is a variable so is placing object
4 Likes

Does your script have collision detection?

Im sorry, what is collision detection?

Does any part of your script check if the object the player is trying to place can be placed in that location?

no, can you help me script that?

Sure. The most simple way I can think of off the top of my head is to use the GetTouchingParts method of BaseParts. You could assign a “hitbox” part to each model, and every time the player tries to place something, you loop through all the parts the hitbox is touching by running the GetTouchingParts method on the hitbox part.

If there are any parts that the hitbox is touching, you can assume that the model is colliding with something. If that happens, you can delete the model that the player is trying to place.

Also a quick side note, I suggest that you make the hitbox slightly smaller than the dimensions of the model. That way, the hitbox won’t collide with the floor.

Do you mind providing an example?

alright give me a second to write it out

Just to clarify, the script you provided is the only script that handles placement? Is there no server sided script to place the object?

1 Like

There is a server im just only showing the part where we change the objects CFrame based on the mouses position

oh wait i misread the post. Is the issue occuring when the player is placing the object or when the preview object is being moved?

1 Like

when the preview object is being moved like when the player changes the mouses positon while they are placing the object, the object gets moved to the mouses position. So yeah when the mouse moves the preview object moves. Hopefully this makes sense im bad at explaining stuff.

So your issue is when the preview object is moved, it can clip through other objects, correct?

1 Like

yes. When the object moves it can be clipped into things like other objects and walls. I want to try and fix this in the client side when the players mouse changes.

You could change it so that instead of setting the CFrame, it uses an AlignPosition. That way, if the model collides with something, the magic of the Roblox physics engine will prevent it from continuing to move forward.

btw ignore everything i said earlier about a collisions system, i misread the post

Oh can you please show an example on like how to use it? Also the model has rotation will that affect anything?

To use AlignPosition, you need a center part that has the AlignPosition inside of it. From the looks of your code, it would be the “MainPart” object.

To get this to work, you will need to put an attachment and an AlignPosition inside of the MainPart object. Set the “mode” property of the AlignPosition to “OneAttachment”, and set the attachment property of the AlignPosition to the attachment you added to the MainPart earlier. Set the “MaxForce” property of the AlignPosition to a really giant number, and keep adding zeros to that number until Roblox Studio displays the number as “inf”.
image

Inside your code, replace the line where it says “previewobject:SetPrimaryPartCFrame” with alignPosition.Position = objectcframe.Position, where the alignPosition variable is the AlignPosition of the MainPart.

Also, while I was testing this method, I noticed that since the AlignPosition is very responsive, the part might get very violently moved to the destination. My solution to this is to use an AlignOrientation to correct the orientation and to set the rotation of the part. To get an AlignOrientation to work, it’s mostly the same steps as the AlignPosition.

With an AlignOrientation, which I will refer to as “AO” for brevity, you will need to add an AO and an attachment into the MainPart, set the AO’s mode to OneAttachment, and set the attachment to the attachment you added to the MainPart. Set the AO’s MaxTorque to a really big number that Studio displays as infinite, and then set the responsiveness property to 200.

As for your code, you will need to add a new line below the one setting the AlignPosition’s position. In that new line, you should write

previewobject.MainPart.AlignOrientation.CFrame = CFrame.new(0,0,0) * objectangles

ALSO, a few KEY THINGS TO NOTE:

  1. The MainPart needs to be unanchored.
  2. If the model has other parts, the other parts need to be welded to the MainPart

If I’m correct, this should be essentially the same as setting the PrimaryPart’s CFrame, except instead of setting an anchored part’s CFrame, you’re instead using the Roblox physics engine.

1 Like

The problem is if the parts are unanchored the placement system glitches out. Is there any other way we can do this by raycasting? Im not good at raycasting but im pretty sure that can work too. Because the part just falls to the floor when its unanchored.

For raycasting, I suppose you could cast a ray from the previewobject’s last position to the mouse’s current position. If the raycast returns a result, then the position of the raycast’s intersection would be where you should set the PrimaryPart’s position to. However, if the raycast doesn’t return anything, you can set the PrimaryPart’s CFrame regularly.

oh, do you mind providing an example? Sorry that I keep on asking for examples.