Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can you make a part follow your mouse?

Asked by 8 years ago

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

Log in to vote
0
Answered by
yoshi8080 445 Moderation Voter
8 years ago
Edited 8 years ago
 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

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. austin10111213 28 — 8y
0
oh I'll fix that yoshi8080 445 — 8y
0
try it now yoshi8080 445 — 8y
0
line 17 the end need a ")" also your condition line 7, remove your "end" line 16 and add an end line 13 XToonLinkX123 580 — 8y
0
I fixed it, I didn't use studio to format it properly which caused these mistakes yoshi8080 445 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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

Answer this question