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

Script runs if only one argument is true, and only the first part works. Thoughts?

Asked by 2 years ago

I'm trying to make it so two different types of grass spawn at different times of day.

local grassTime = game.Lighting:GetMinutesAfterMidnight()

local sunGrass = game.ServerStorage.SunGrass
local moonGrass = game.ServerStorage.MoonGrass

local function spawnSunGrass()
    local sunCopy = sunGrass:Clone()
    sunCopy.Parent = game.Workspace.SunGrass
    local xPos = math.random(-3500, 3500)/100
    local zPos = math.random(-3500, 3500)/100
    sunCopy.Position = Vector3.new(xPos, 1, zPos)
end

local function spawnMoonGrass()
    local moonCopy = moonGrass:Clone()
    moonCopy.Parent = game.Workspace.MoonGrass
    local xPos = math.random(-3500, 3500)/100
    local zPos = math.random(-3500, 3500)/100
    moonCopy.Position = Vector3.new(xPos, 1, zPos)
end

--Problems start here.
while true do
    grassMath = math.random(1, 50)
    if grassMath == 1 and grassTime >= 480 and grassTime <= 1320 then
        spawnSunGrass()
        wait()
    end

    if grassMath == 2 and grassTime <= 480 and grassTime >= 1320 then
        spawnMoonGrass()
        wait()
    end
end

I've tried doing an if statement for each thing, one for grassMath and two for grassTime. I get all sorts of different results by changing bits and pieces, and have no clue where it's going wrong.

0
I am not sure if this will work but have you tried elseif instead of 2 separate if statements? supertarek5000 20 — 2y

2 answers

Log in to vote
0
Answered by
AronYstad 101
2 years ago

Do you mean that this part doesn't work?

if grassMath == 2 and grassTime <= 480 and grassTime >= 1320 then
    spawnMoonGrass()
    wait()
end

If so, it might be because grassTime can't be less than or equal to 480 and greater than or equal to 1320 at the same time. If you want it to spawn if grassTime is less than or equal to 480 or when grassTime is greater than or equal to 1320, I would use this code:

if grassMath == 2 and grassTime <= 480 then
    spawnMoonGrass()
    wait()
elseif grassMath == 2 and grassTime >= 1320 then
    spawnMoonGrass()
    wait()
end

or

if grassMath == 2 then
    if grassTime <= 480 or grassTime >= 1320 then
        spawnMoonGrass()
        wait()
    end
end
Ad
Log in to vote
0
Answered by 2 years ago

The problem was it never got past trying to spawn SunGrass, even if it wasn't within its set spawn time. I've figured out a different method that works, but if anyone has an answer using the stuff above I'd also like to know.

Here's how I fixed it:

local Lighting = game:GetService('Lighting')
local sunTimeStart = 8
local sunTimeEnd = 22
local moonTimeStart = 22
local moonTimeEnd = 8
local total = game.ReplicatedStorage.TotalGrass

--Functions are the same so won't include those.

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    grassMath = math.random(1, 2500)
    wait()
    if grassMath == 1 then
        if Lighting.ClockTime >= sunTimeStart and Lighting.ClockTime <= sunTimeEnd then
            spawnSunGrass()
            total.Value += 1
        else
            wait()
        end
    else
        wait()
    end

    if grassMath == 2 and  Lighting.ClockTime <= moonTimeEnd then
        spawnMoonGrass()
        total.Value += 1
        wait()
    elseif grassMath == 2 and Lighting.ClockTime >= moonTimeStart then
        spawnMoonGrass()
        total.Value += 1
        wait()
    end
end)

I included the first solution you sent for fixing MoonGrass, thank you. :)

Answer this question