Object Placement occasionally doesn't work

Essentially what I am trying to accomplish is a block placement system, in which you can place 4x4x4 blocks by clicking. However, it occasionally just stops working. However, even then, it still prints “Test” even while doing it, meaning that it should be placing blocks.

The video that displays this can be seen here. (the file size is too large to fit in here)

If you did watch the video, you could see that it would work however sometimes it would also just refuse to place any blocks whatsoever, regardless of if its in range.

(Keep in mind that the code is client-sided for now, once I figure out how to properly place blocks I will make it Filter-Enabled compatible.)

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local Tool = script.Parent

function roundBlockPlacement(Position, Object)
	local X,Y,Z
	
	X = math.floor(Position.X / Object.Size.X + .5) * Object.Size.X
	Y = math.floor(Position.Y / Object.Size.Y + .5) * Object.Size.Y 
	Z = math.floor(Position.Z / Object.Size.Z + .5) * Object.Size.Z  
	
	return Vector3.new(X,Y,Z)
end

function toolActivated()
	if (Mouse.Hit.Position - HumanoidRootPart.Position).Magnitude <= 40 then
		print("Test")
		
		local mouseRay = Mouse.UnitRay 
		local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.Unit * 40) 
		local touchedPart, mousePosition, partLookVector = workspace:FindPartOnRay(newRay)
		
		local Part = Instance.new("Part")
		Part.Size = Vector3.new(4,4,4)
		Part.Anchored = true
		
		if touchedPart and mousePosition and partLookVector then
			local partPosition = roundBlockPlacement(mousePosition + (partLookVector * (Part.Size *.5)), Part) 
			
			Part.CFrame = CFrame.new(partPosition)
			Part.Parent = workspace
		end
	end
end

Tool.Activated:Connect(toolActivated)

your print(“Test”) prints only when the player is closer than or equal to 40 studs from the mouse position when you click, put it to where it creates the part and to where it moves the part to and see if it still prints test

1 Like

so basically, I create the part and place it, and then afterwards check if the part is within the 40 stud radius?

No, you said your print(“Test”) works so it should be placing the part but that is not true it’ll print Test every time as long as the player is closer than or equal to 40 studs from the mouse so put print(“Test”) under Part.Anchored = true and Part.Parent = workspace and see if it still prints Test

1 Like

Tried it, and it still prints Test regardless

Did you try to just remove the distant check? Besides that I am not seeing anything really that would cause a problem like that

Actually, yes. I thought that if I clicked beyond 40 studs it would just stop working altogether, so I just put a conditional statement to see if it was less than or equal to 40.

If anyone is wondering, I did fix it. However, I did an entirely different procedure in terms of block placement.

local Target = Mouse.Target
local Face = Mouse.TargetSurface
	
local Part = Instance.new("Part")
Part.Size = Vector3.new(3,3,3)
Part.Anchored = true
Part.Position = Target.Position + (Vector3.FromNormalId(Face) * Vector3.new(3,3,3))

Part.Parent = workspace

However from what I can tell, this script only works when it is placed on an object that has the size multiple of its own size (its worded very strange, but basically if the part size is 3 studs on all axes, you can only place it on blocks that have 9 studs on all axes. This may be wrong, though I can’t really come up with any other feasible reason why this is happening.)

3 Likes