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

I didn't think making a fountain was going to be this hard?

Asked by 8 years ago
local fountain  =script.Parent
local a = 0
local b = 0
local debounce = false

p = math.random(3)
o = math.random(3)
y = math.random(3)

while b == 0 do
    p = math.random(3) + math.random(3)
    b = math.random(3) + math.random(3)
    y = math.random(3) + math.random(3)
    wait(.1)
end

while a == 0 do
    local part = Instance.new("Part")
    part.Size = Vector3.new(1,1,1)
    part.Shape = Enum.PartType.Block
    part.Transparency = 0.5
    part.BrickColor = BrickColor.new("Deep blue")
    part.Velocity = Vector3.new(0,10,0)
    part.CFrame = CFrame.new(fountain.Position + Vector3.new(p,o,y))
    part.Parent = game.Workspace
    wait(.1)
end

I am trying to make it so when a block spawns, the water block, it spawns randomly every time it spawns, but once it spawns somewhere, it seems to spawn from that location only... I'm not too good with math.random as you can see...

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

What is happening is you're getting the initial p o y values which is the location where the part will spawn. You then go into a while loop where it sets the value of b to be a random value. This will cause the while loop to terminate as you have the condition for the while b == 0. So the while loop will repeat as long as b remains zero which is won't for long.

You might also start to see that parts will only spawn on one side of the fountain even if you got the script working.


Solution

What you would need to do is use one while loop. That while loop will not terminate and the fountain will consistently run. You would need to move the random functions into that while loop as well to ensure they update.

I would advise using negative values for a fountain to prevent the parts from spawning on one side. If you do not like this feature, then you are welcome to remove it.


Final Script

local fountain = script.Parent
local a = 0
--local b = 0 --Changes made this variable obsolete.
--local debounce = false --There really is no need for this variable.

while a == 0 do

    p = math.random(-3, 3)
    o = math.random(-3, 3)
    y = math.random(-3, 3)

    local part = Instance.new("Part")
    part.Size = Vector3.new(1,1,1)
    part.Shape = Enum.PartType.Block
    part.Transparency = 0.5
    part.BrickColor = BrickColor.new("Deep blue")
    part.Velocity = Vector3.new(0,10,0)
    part.CFrame = CFrame.new(fountain.Position + Vector3.new(p,o,y))
    part.Parent = game.Workspace
    wait(.1)
end

Hopefully this answered your question, if it did do not forget to hit the accept answer button. If you have any questions, feel free to comment below and I will get back to you.
Ad

Answer this question