I was making module that spawns burger randomly in the map.
But whenever i put print in the code, it works good. But if i don’t, it doesnt work.
These are the codes.
-- Module Script in Server Storage
local module = {}
local basePart = workspace.BaseGame
local function GetColorFromBurgerStat(number)
if number > 250000000 then -- 250m~1b
return BrickColor.new(Color3.new(0.482353, 1, 0.956863))
elseif number > 100000000 then -- 100m~249.99m
return BrickColor.new(Color3.new(0.882353, 0, 1))
elseif number > 10000000 then -- 10m~99.99m
return BrickColor.new(Color3.new(1, 0.0313725, 0))
elseif number > 1000000 then -- 1m~9.99m
return BrickColor.new(Color3.new(0.333333, 0, 0.498039))
elseif number > 100000 then -- 100k~999k
return BrickColor.new(Color3.new(0, 0, 0))
elseif number > 10000 then -- 10k~99.99k
return BrickColor.new(Color3.new(0.172549, 1, 0.113725))
elseif number > 1000 then -- 1000~9999
return BrickColor.new(Color3.new(0.988235, 1, 0.341176))
elseif number > 100 then -- 100~999
return BrickColor.new(Color3.new(1, 0.701961, 0))
else -- 0~99
return BrickColor.new(Color3.new(1, 1, 1))
end
end
local function spawnBurger()
local spawnHeight = 100
local fallSpeed = 70
local burgerTable = {
["Basic Burger"] = "1/99",
["Common Burger"] = "100/499",
["Uncommon Burger"] = "500/999",
["Rare Burger"] = "1000/1499",
["Legendary Burger"] = "1500/2499",
["Impossible Burger"] = "2500/24999",
["Insane Burger"] = "25000/100000",
}
local newPosX = basePart.Position.X + math.random(-basePart.Size.X/2, basePart.Size.X/2)
local newPosZ = basePart.Position.Z + math.random(-basePart.Size.Z/2,basePart.Size.Z/2)
local newPosY = basePart.Position.Y + basePart.Size.Y / 2 + spawnHeight
local newPos = Vector3.new(newPosX, newPosY, newPosZ)
local burgerName
local randomIndex = math.random(1, 200)
if randomIndex == 1 then
burgerName = "Insane Burger"
elseif randomIndex <= 3 then
burgerName = "Impossible Burger"
elseif randomIndex <= 7 then
burgerName = "Legendary Burger"
elseif randomIndex <= 14 then
burgerName = "Rare Burger"
elseif randomIndex <= 22 then
burgerName = "Uncommon Burger"
elseif randomIndex <= 72 then
burgerName = "Common Burger"
else
burgerName = "Basic Burger"
end
local newBurger = script.Burgers[burgerName]:Clone()
local min = tonumber(string.split(burgerTable[burgerName], "/")[1])
local max = tonumber(string.split(burgerTable[burgerName], "/")[2])
local random = math.random(min, max)
newBurger.MeshPart.BurgerInfo.PlayerTime.TextColor = GetColorFromBurgerStat(random)
newBurger.MeshPart.BurgerInfo.PlayerTime.Text = tostring(random)
newBurger.Parent = workspace
newBurger.Handle.CFrame = CFrame.new(newPos)
-- the mystery starts from here
print(newBurger.Handle.CFrame) -- this line
print(newPos) -- and this line
local att1 = Instance.new("Attachment")
att1.Parent = workspace.BaseGame
att1.Position = Vector3.new(newPosX, basePart.Position.Y, newPosZ)
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.Parent = newBurger.Handle
linearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
linearVelocity.Attachment0 = newBurger.Handle.Attachment
linearVelocity.MaxForce = math.huge
linearVelocity.LineVelocity = fallSpeed
linearVelocity.LineDirection = Vector3.new(newPosX, newPosY - spawnHeight, newPosZ) - newPos
local alignOri = Instance.new("AlignOrientation")
alignOri.Parent = newBurger.Handle
alignOri.Mode = Enum.OrientationAlignmentMode.OneAttachment
alignOri.MaxTorque = math.huge
alignOri.MaxAngularVelocity = math.huge
alignOri.Attachment0 = newBurger.Handle.Attachment
local fallTime = spawnHeight / fallSpeed + 0.3
game.Debris:AddItem(linearVelocity, fallTime)
game.Debris:AddItem(alignOri, fallTime)
game.Debris:AddItem(att1, fallTime)
end
function module:SpawnBurger()
local playerSum = #game.Players:GetPlayers()
for i=1, playerSum, 1 do
spawnBurger()
end
end
return module
-- Script in Server Script Sercice
local burgerSystem = require(game.ServerStorage.BurgersSystem)
local spawnTime = 4
while task.wait(spawnTime) do
burgerSystem:SpawnBurger()
end
This is when i don’t put print.
robloxapp-20230521-1238506.wmv (1.1 MB)
The burgers spawns in same position every time. And some of them just disappears when it spawns.
And this is when i put print.
robloxapp-20230521-1242217.wmv (4.1 MB)
It works very good.
Please help me.