|
| 12 Nov 2016 07:19 PM |
I'm trying to make a game and I have a button on a gui to activate placing a brick. The problem is that when I double click the button I can place bricks infinitely. I want it to let me click, place the brick, then repeat. It does that if I click the button once, but hitting more than once makes it infinite. Here's the script. Also, it would be awesome if you could optimize it if you can!
Player = game.Players.LocalPlayer serverstoragevar = game:GetService("ServerStorage") placing = nil frame = game:GetService("StarterGui").ScreenGui.Frame
function placeunit() local Mouse = Player:GetMouse() mousedown = Mouse.Button1Down:connect(function() if Mouse.Target ~= nil then if placing ~= true then placing = true local unitSelected = serverstoragevar.InfUnits["Heavy Infantry"] local createdUnit = unitSelected:Clone() createdUnit.Parent = game.Workspace createdUnit:SetPrimaryPartCFrame(Mouse.Hit) placing = false mousedown:disconnect() end end end) end
script.Parent.MouseButton1Click:connect(placeunit) |
|
|
| Report Abuse |
|
|
VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
|
| 12 Nov 2016 07:38 PM |
Not sure what exactly goes wrong and what do you expect. Does it spawn more than one brick at once? Do you want to create one brick per click? Do you want to turn off the button after the first click?
Why do you connect to Mouse.Button1Down? It would fire immediately since the user just clicked the button, wouldn't it? Do you want the user to press the button and then click elsewhere to create a brick there? If you do, you could use PlayerMouse.Changed instead. |
|
|
| Report Abuse |
|
|
|
| 12 Nov 2016 07:45 PM |
| When I click the button, I want to be able to place one brick then have to click the button again. I do want the user to click the button to click elsewhere to place the brick. |
|
|
| Report Abuse |
|
|
|
| 12 Nov 2016 07:48 PM |
| Can you make an example script with the PlayerMouse.Changed? |
|
|
| Report Abuse |
|
|
VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
|
| 12 Nov 2016 08:05 PM |
Nevermind, PlayerMouse.Changed fires when a property changes, and Mouse doesn't have a property like Button1IsDown or something.
You need another variable. It should be set to true when the user presses the button and set to false when they create a brick. Maybe you won't need to change anything else, though I'm not sure why doesn't mouse.Button1Down fire immediately after the button is pressed. |
|
|
| Report Abuse |
|
|
|
| 12 Nov 2016 08:07 PM |
| I think it doesn't fire immediately after because it's called after you press the button, so there is a small delay between the button and the event. Thanks for the help! |
|
|
| Report Abuse |
|
|
| |
|
VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
| |
|