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

It doesn't work multiple times?

oils = game.Workspace.Oils:GetChildren()
local t = {false,true}
placeable = t[math.random(1,2)]
teamName = "Really blue"
building = game.Workspace.Model:Clone()
mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:connect(function()
for q=1,#oils do
		if not placeable then break end
		if not oils[q]:FindFirstChild(teamName) and (building.Base.Position-oils[q].Position).magnitude <= 10 then
			placeable = true oilNum = q break
		else placeable = false
		end
	end
end)

This only works for one oil spot. When the model moves close enough to one oil spot, it makes placeable true, when it navigates close to the second spot, it doesn't make placeable true. I've spent hours trying to fix this, and I couldn't figure anything out.

4 comments

0

most likely because you broke out of the loop, which then makes placeable false;

0

It breaks, and makes placeable true.

0

Once placeable gets set to false, line 10 makes the loop break without ever being able to set placeable back to true

0

It sets to true, it breaks, it repeats.

1 answer

✓ 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
	-- place building
end

2 comments

0

Would my placeable work? Because I have A LOT of other things to check if they can build the building.

0

Well yeah, you could do the same thing I did here, except instead of setting the oilSpot variable you could set a placeable variable. You would write placeable = false where I put oilSpot = nil and placeable = true where I put oilSpot = spot.

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