Tool On Its X and Z position. (Maybe Y?)

Hello! Again. (or if not, welcome.) And I post daily scripting, building, developing, ect problems.
Today’s problem is kind of the same as last times. Still. Like the last one, I will give out another thanks. It is your chance. (I think).

When I move my mouse it appears a brick. And it follows your every move. Like build a boat, building tool but without the mouse. Thanks to these two people, “roasthistorychicken” and “Danrich123” for helping me with the Y axis. Here’s another example. (It is kinda laggy.)

Please help and respond. :slight_smile:

script:

local player = game.Players.LocalPlayer
local workspacepart = game:GetService("Workspace")
local partsgo = Instance.new("Model")
	local playerbuild = Instance.new("Model")
	playerbuild.Parent = workspacepart
	playerbuild.Name = (player.Name.." Space")
	partsgo.Name = "ModelParts"
	partsgo.Parent = workspacepart
local madepart = Instance.new("Part")
	madepart.Parent = partsgo
	madepart.Size = Vector3.new(3,3,3)
	madepart.Name = (player.Name.." Part")
	madepart.Anchored = true
local mouse = player:GetMouse()

mouse.TargetFilter = madepart; --ignores the part you're moving (so it doesn't make a weird thing

while true do
	wait()
	--madepart.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z) + Vector3.new(0, madepart.Size.Y / 2, 0)
	madepart.CFrame = CFrame.new(math.ceil(mouse.Hit.p.X), 3, math.ceil(mouse.Hit.p.Z))
end

PLEASE READ:

When you fix the X and Z positions, there might be another problem, too. See how you had to add a + at the mouse.hit.Y? When you hover your cursor over a roof for example, a house, it will probably go up. Please fix.

PROBLEM:

When the mouse moves to a X and Z position, it won’t go up. Same with a roof. Also collides with wall.

I believe you have set the Y value to a solid 3.
You have also commented out something that seems to be 100 percentoe correctoe.

Please may you bestow me with your “thanks”
Love Simon.

1 Like

Change that to this:

CFrame.new(math.ceil(mouse.Hit.p.X), math.ceil(mouse.Hit.p.Y), math.ceil(mouse.Hit.p.Z))

You can also do this, which I find much cleaner:

local Part = Instance.new("Part", workspace)

Part.CanCollide = false

local Mouse = game.Players.LocalPlayer:GetMouse()

local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function()

    Part.CFrame = CFrame.new(math.ceil(Mouse.Hit.p.X), math.ceil(Mouse.Hit.p.Y), math.ceil(Mouse.Hit.p.Z))

end)
1 Like

Okay… Thanks for responding, but what script line?

I will try that clean method. I am going to see if it works.

TOP_Crundee123, when you put your cursor on the roof, ot just does the same with the bottom floor.

What do you mean by, “Roof”? If your are saying what will happen, then the same thing will happen on the roof and on the floor. The part goes to the mouse position.

I mean roof as in top (the opposite of floor). And i tested it.

yes, it does work on the roof also

The clean method? Or the other one? Or both. Cause I tried it like 4 times.

Let me show you a vid. Get ready.


local player = game.Players.LocalPlayer

local workspacepart = game:GetService("Workspace")

local partsgo = Instance.new("Model")
	partsgo.Name = "ModelParts"
	partsgo.Parent = workspacepart

local playerbuild = Instance.new("Model")
	playerbuild.Parent = workspacepart
	playerbuild.Name = (player.Name.." Space")
	
local madepart = Instance.new("Part")
	madepart.Parent = partsgo
	madepart.Size = Vector3.new(3,3,3)
	madepart.Name = (player.Name.." Part")
	madepart.Anchored = true
	
local Mouse = player:GetMouse()

local RS = game:GetService("RunService")

Mouse.TargetFilter = madepart;

RS.RenderStepped:Connect(function()

    madepart.CFrame = CFrame.new(math.ceil(Mouse.Hit.p.X), math.ceil(Mouse.Hit.p.Y), math.ceil(Mouse.Hit.p.Z))

end)

try using the cleaner method that I gave

I did! I gave you the script. And you seen it. Right?

Yes, but you don’t need some of the script

It’s for my game. Here is everyline needed:

local partsgo is the model the part goes in. madepart is the part you drag. local mouse, RS, target filter, and renderstepped.

Edit: I removed playerbuild

Found a fix. Credit to @EgoMoose.

local player = game.Players.LocalPlayer

local partFolder = Instance.new("Folder") -- folders are better
partFolder.Name = "ModelParts"
partFolder.Parent = workspace

local playerBuild = Instance.new("Folder")
playerBuild.Parent = workspace
playerBuild.Name = player.Name .. " Space"

local moveModel = Instance.new("Model")
moveModel.Parent = partFolder

local part = Instance.new("Part") -- turned into a model just for you, it works significantly better
part.Parent = moveModel
part.Size = Vector3.new(3, 3, 3)
part.Name = player.Name .. " Part"
part.Anchored = true

moveModel.PrimaryPart = part

local mouse = player:GetMouse()

local function ceilVector3(vec)
    return Vector3.new(math.ceil(vec.X), math.ceil(vec.Y), math.ceil(vec.Z))
end

function placeAtMouse(dt) -- egomoose's solution
    if (mouse.Target) then
        -- where we cast our ray from (shifted slightly up so the ray will hit the surface we're hovering)
        local origin = mouse.Hit.p + Vector3.new(0, 0.1, 0)
 
        -- cast our ray and get our normal, which is a unit vector
        local ray = Ray.new(origin, Vector3.new(0, -1, 0))
        local hit, pos, normal = workspace:FindPartOnRay(ray, moveModel)
 
        -- get the perpendicular vector and the angle between the two
        local cross = Vector3.new(0, 1, 0):Cross(normal)
        local theta = math.acos(Vector3.new(0, 1, 0):Dot(normal))
        
        -- make sure our axis is valid
        local axis = Vector3.new(1, 0, 0)
        if (cross.magnitude > 0) then
            axis = cross.unit
        end
 
        -- CFrame the model
        moveModel:SetPrimaryPartCFrame(
            CFrame.new(mouse.Hit.p + normal * moveModel:GetBoundingBox().Y / 2) -- position
            * CFrame.fromAxisAngle(axis, theta) -- rotation
        )
    end
end
 
-- update every frame
game:GetService("RunService").RenderStepped:connect(placeAtMouse)

Edit: Modified it to do the whole math.ceil thing.

Thanks! :smiley: I have never got this write.