Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can you make a part follow your mouse?

Hi, I'm trying to create a build preview. Where a transparent version of the part appears and follows your mouse until you like where it is then when you click it places it in that position. The problem I'm having is when you click the button to start the build preview it just places the part where you clicked it doesn't follow the mouse at all.

Here is the script I'm using, this is a localscript located inside a GUI textbutton:

script.Parent.MouseButton1Down:connect(function()
	GUI = script.Parent.Parent.Parent.Parent
	Wall = game.ServerStorage.WoodWall
	Player = game.Players.LocalPlayer
	Mouse = Player:GetMouse()
	
	if Player.leaderstats.Wood.Value >= 5 then
		Player.leaderstats.Wood.Value = Player.leaderstats.Wood.Value -5
		GUI.Visible = false
		WWall = Wall:Clone()
		WWall.Parent = game.Workspace
		WWall.Transparency = .5
		WWall.Position = Mouse.hit.p
	end 
	
end)

2 answers

 GUI = script.Parent.Parent.Parent.Parent
    Wall = game.ServerStorage.WoodWall
    Player = game.Players.LocalPlayer
    Mouse = Player:GetMouse()

script.Parent.MouseButton1Down:connect(function()
    if Player.leaderstats.Wood.Value >= 5 then
        Player.leaderstats.Wood.Value = Player.leaderstats.Wood.Value -5
        GUI.Visible = false
        WWall = Wall:Clone()
        WWall.Parent = game.Workspace
        WWall.Transparency = .5
	end
end)

Mouse.Move:connect(function()
    WWall.CFrame = CFrame.new(Mouse.Hit.p.x,1,Mouse.Hit.p.z)
end) 

Using the Mouse movie function, it'll constantly move the part on the x and z axis, to prevent the part from moving up the y axis.

EDIT: reformatted script to prevent more errors

5 comments

0

That didn't work that just made the part spawn in the position it was made in when i moved it into server storage.

0

oh I'll fix that

0

try it now

0

line 17 the end need a ")" also your condition line 7, remove your "end" line 16 and add an end line 13

0

I fixed it, I didn't use studio to format it properly which caused these mistakes

Yeah, the problem is the script that is in the mouse click function is only activating it when the player clicks. so of course it won't follow the mouse, I recommend doing what yoshi said and do a mouse move event, otherwise it won't work.

Just to clear things up if you didn't get why it didn't work exactly

This discussion is preserved as it was. New answers, comments, and votes are closed.