Scripting mystery

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.

1 Like

I think its because the Physics engine not having enough time to compute the CFrame change when you are already applying the LinearVelocity and AlignOrientation which uses the Physics engine too.

That happens too when you try to change the CFrame of the HRP of a player that joined, even if the character fully loaded it needs a little “wait” before to be able to change its CFrame. I fixed that issue by just waiting a step of the RunService.Heartbeat on server.

The prints you added actually creates a little “wait” before applying the Linear and Align, thats why it works when it has the prints and wont work if you dont have them.

You could remove the prints and edit the code part when you are Parenting the Burger to workspace and when you are changing the CFrame:

newBurger.Parent = workspace
newBurger.Handle.CFrame = CFrame.new(newPos)
game:GetService("RunService").Heartbeat:Wait()

I think that will solve “the mystery” xD
If it doesnt try by adding a second HeartBeat Step Wait() after the Parenting


Sidenote, I tested your scripts and works fine for me, with and without the Print lines. I expected that, cause Im testing it in a empty baseplate without any other scripts dealing with Physics so the engine is not “stressed” at all, cause its not handling anything else but your scripts. And my Burger model is just a Model with a Part called Handle, no extra meshes in it.

Another thing you could do to fix it its improving the Meshes inside the Burger model, make sure it has properties like Massless True, CanCollide False, CanTouch False, CanQuery False. All meshes inside the model should be like that so those wont interact with the Physics engine. The only part that should interact with the Physics is the Handle part, and all other meshes should be welded to that Handle part, and the Model should have that Handle as its PrimaryPart

2 Likes

OMG It works good now.
Thanks for your help peashie :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.