Math.random errors when generating between negative and positive numbers

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I wanna make it so that fireballs fall across the basplate
  2. What is the issue? Include screenshots / videos if possible!
    I am getting this error: invalid argument #2 to ‘random’ (interval is empty)
    I am trying to get a random number between a negative number and a positive number
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I did but they were only dealing with postive numbers
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local isInAir = true
local onGround = false
local debounce = false
local ground = workspace.Baseplate
--dimensions of the baseplate
local minX = 47.568
local maxX = -178.151
local minZ = -149.885
local maxZ = 88.1
local rng = Random.new(tick())
script.Parent.Position = Vector3.new(math.random(maxX, minX), script.Parent.Position.Y, math.random(maxZ, minZ)) --errors
script.Parent.Touched:Connect(function(touched)
	if not debounce and touched == ground then
		debounce = true
		onGround = true
		isInAir = false
		local sound = Instance.new("Sound")
		sound.SoundId = "rbxassetid://" .. 262154437
		sound:Play()
		sound.Ended:Connect(function() sound:Destroy() end)
	end
end)
if onGround and not isInAir then
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 1200
	explosion.BlastRadius = 250
	explosion.Position = script.Parent.Position
	explosion.Hit:Connect(function(hit, distance)
		if hit.Parent:FindFirstChild("Humanoid") ~= nil then
			hit.Parent:FindFirstChildWhichIsA("Humanoid").Health = 0
			if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
				game.Players:GetPlayerFromCharacter(hit.Parent):Kick("A fireball crushed you.")
			end
		end
	end)
	local crater = game.ServerStorage.Assets.Crater:Clone()
	local craterPrimaryPart = crater.PrimaryPart
	for i, v in pairs(crater:GetChildren()) do
		if v ~= crater.PrimaryPart then
			local weld = Instance.new("WeldConstraint")
			weld.Part0 = v
			weld.Part1 = craterPrimaryPart
		end
	end
	craterPrimaryPart.Position = script.Parent.Position
	crater.Parent = workspace
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Because you are setting the 1st argument lower than the 2nd argument, this is what you are doing:

math.random(88.1, -149.885)

Which isnt how that works
It should be this:

math.random(-149.885, 88.1)
-- which is this:
math.random(minZ, maxZ)

thank you, this post wouldn’t exist if I didn’t skim over my code

Also, another thing.

This wont affect math.random, you would need to use math.randomseed to alter it, or use one of the functions Random contains.

math.randomseed(tick()) -- effects math.random
math.random(min, max)

Random.new(tick()):NextInteger(min, max)

i was scrolling through google and saw one suggestion but it was the same thing
my bad

1 Like

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