✓ Accepted answer
##Problems
The reason this script works the first time is because math.random(1, 2) will always return 2 with the default seed, so the variable placeable always starts off as true.
The reason this script stops working is because once placeable is set to false, it remains that way as you never give it a chance to see if it should be set to true.
Inside your mouse.Move function you have a lot of unnecessary boolean logic going on, so I'm going to help you simplify it.
##Solution
Instead of a placeable variable, I'm going to use a variable the holds the oil spot is could currently be placed onto. If it can not be placed, this value will be set to nil.
Now, everytime the mouse is moved, we will set this value to nil. Once we do that we can loop through all the oil spots to see if any of them meet our requirements. If there is a valid spot available, we will set the variable to this spot.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local oilSpots = game.Workspace.Oils:GetChildren()
local building = game.Workspace.Model:Clone()
local oilSpot = nil
mouse.Move:connect(function()
oilSpot = nil
for _, spot in next, oilSpots do
if not spot:FindFirstChild(player.TeamColor) and (building.Base.Position - spot.Position).magnitude <= 10 then
oilSpot = spot
break
end
end
end)
Now that you have that you can write your function to place it. All you have to do is check if the variable 'oilSpot' is not nil, and place the building.
if oilSpot then
end
4 comments
most likely because you broke out of the loop, which then makes placeable false;
It breaks, and makes placeable true.
Once placeable gets set to false, line 10 makes the loop break without ever being able to set placeable back to true
It sets to true, it breaks, it repeats.