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

Script doesn't put brick in random spot??

Asked by
7_c 37
7 years ago

So basically I made a script that puts a brick in a random spot on the map using math.random. The problem is that it doesn't work when I tested it.

local locations = math.random(1,2)

while true do
    wait(.5)
    if game.Lighting.TimeOfDay == "02:00:00" or game.Lighting.TimeOfDay == "01:00:00" or game.Lighting.TimeOfDay == "00:00:00" then
        if locations == 1 then
            clone = script.Parent:Clone()
            clone.Position = clone.Position + Vector3.new("232.572, 10.875, 384.915")
            if game.Lighting.TimeOfDay == "14:00:00" then
            clone:remove()
            if locations == 2 then
            clone2 = script.Parent:Clone()
            clone2.Position = clone2.Position + Vector3.new("-8.428, 10.875, 244.915")
                        if game.Lighting.TimeOfDay == "14:00:00" then
            clone2:remove()
                        end

            end
        end
    end
end

1 answer

Log in to vote
0
Answered by 7 years ago

Put locations in the while loop, before or after the wait.

You have to do this so the variable updates every loop.


Wrong example:

-- Wrong way example:
local random = math.random(2)

-- This will either always print 1, or always print 2.
while true do
    print(random)
    wait()
end

Correct example:

-- Always prints random number, 1 or 2.
while true do
    local random = math.random(2)
    print(random)
    wait()
end

P.S.

Always test your game in an online server when possible, especially when using math.random.

Ad

Answer this question