Issue with my code

if placingobject == true then
	mouse.TargetFilter = previewobject

	if previewobject:FindFirstChild("MainPart") then
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Exclude
		rayParams.FilterDescendantsInstances = {previewobject}
		
		local direction = -(previewobject.MainPart.Position - mouse.Hit.Position)
		local result = workspace:Raycast(previewobject.MainPart.Position, direction, rayParams)
		
		if not result 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:PivotTo(objectcframe*objectangles)
		else
			-- it's collided with something, so we need to move it to the destination
			local objectangles = CFrame.Angles(0, math.rad(rotationamount), 0)
			local objectcframe = CFrame.new(result.Position) * objectangles
			
			previewobject:PivotTo(objectcframe)
		end
	end
else
	previewobject:Destroy()
end

There are a few issues with trying to raycast to the mouseā€™s position, mainly being that in some cases the model will be ā€œstuckā€ on walls, but it should generally work fine.

Also, if my code doesnā€™t work, just change the direction variable so that it doesnā€™t have a negative sign in front of it. I havenā€™t tested the code.

1 Like

Thank you! Im gonna go test the code out right now

So the problem is that the placing system is indeed fine but I added this system where it saves and loads the data to the saved CFrame. The problem is sometimes if the parts are too close together they go ontop of the roof. So is there anyway to fix this? I tried canquerry but that messes up my scripts. I wanted to send you a video but the file was too large. But basicly I dont know how to fix it. Like is there a way to just keep them in place because all of the parts are anchored

If theyā€™re anchored, I donā€™t think that should happen. If you could send a code snippet, that would be wonderful. Iā€™d be glad to take a look at it.

1 Like

Ok here is what I did to load the data. But first i need to give you some background info. loadingdata is using datastore:GetAsync() to retrieve the data. When I saved the data I saved the postion(x y z) with split[1] split[2] split[3] and the name is split[4] and orientation split[5] split[6] split[7] I seperated each of the values with spaces. There are no errors though so the main part should work but here is the code:

		for _, l in pairs(loadingdata) do
			print(l)
			local split = string.split(l, " ")
			print(split[1])
			local item = game.ReplicatedStorage.ObjectFolder:FindFirstChild(split[4])
			if item then
				print("item")
				local clone = item:Clone()
				clone.MainPart.CanCollide = false
				clone.Parent = works
				
				
				
				
				-- Calculate the difference in Z-axis between the new player's plot position and the old position
				-- Calculate the differences in axes
				local zDifference = math.round(pos - tonumber(split[3]))
				local xdifference = math.round(posx - tonumber(split[1]))
				local ydifference = math.round(posy - tonumber(split[2]))

				-- Debug print statements
				print("posx:", posx)
				print("posy:", posy)
				print("pos:", pos)
				print("split[1]:", tonumber(split[1]))
				print("split[2]:", tonumber(split[2]))
				print("split[3]:", tonumber(split[3]))
				print("zDifference:", zDifference)
				print("xdifference:", xdifference)

				-- Rest of the code...


				-- Define a threshold value (in studs) for whether to move the models or not
				local threshold = 99

				-- If the Z-axis difference is greater than or equal to the threshold, move the models
				-- Otherwise, keep them at the same position
				--local maths = zDifference >= threshold and zDifference or 0

				-- Calculate the adjusted positions based on the Z-axis difference
				local position2 = Vector3.new(tonumber(split[1]), tonumber(split[2]), tonumber(split[3]))
				--if zDifference >= threshold or zDifference <= 0 then
					--position2 = Vector3.new(tonumber(split[1]), tonumber(split[2]), tonumber(split[3])+zDifference)
				--else
					--warn("no difference")
				--end
				

				-- Define the orientation of the model (you already have it in the split table)
				local orien = Vector3.new(tonumber(split[5]), tonumber(split[6]), tonumber(split[7]))
				
				
				
				local MainFrame = base.CFrame*CFrame.new(position2) * CFrame.Angles(math.rad(orien.X), math.rad(orien.Y), math.rad(orien.Z))
				
				--local realframe = base.CFrame:ToObjectSpace(MainFrame)
				
				clone:PivotTo(MainFrame)

				
				
				
				clone:MoveTo(clone.MainPart.Position)
			else
				warn("cannot find item "..tostring(split[4]))
			end
		end
	else
		warn("loading data = nil")
	end 

oh wait now that im looking at the code maybe the :MoveTo() function is causing this let me quickly test that out and tell you what happens

Ive just found the solution to this! The :MoveTo() function was messing everything up. Thank you so much for you help, you responded very quickly! I will message you if something goes wrong. Thank you again!

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