Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Random math doesn't work randomly ?

I am trying to make a part that has a 50% chance of spawning a wood on it's position, but when I copy more of the blocks and start the game it just spawns on all of them or on none, no erros, and I have no idea how would I fix it

The block is just a half transparent part with a single script in it, the wood is in replicatedstorage

math.randomseed(tick())

local random = math.random(1,400)

if random > 1 and random < 200 then
	local wud = game.ReplicatedStorage.Wood:Clone()
	wud.Parent = game.Workspace
	wud:MoveTo(script.Parent.Position)

elseif random > 200 and random < 400 then
	print("nothing")
	
	
end

script.Parent:Destroy()

6 comments

0

i think u should put a space after math.randomseed and math.random

0

Like this ? "math.random (1,400)" instead of this ? "math.random(1,400)"

0

If yes that didn't make any difference

0

Correct me if im wrong but math.random(1,2) is a 50/50 chance.. you have 50% chance to get either 1 or 2 so why wont you just do that instead? If its 1 then spawn a wood and if its not 1 you dont even need to specify anything else.. it doesnt know what to do when its 2 so it wont do anything so no need for that print("nothing")

0

Okay, I will try that

0

Nope, still does the same, I copy and paste the parts like 20 of them, once they all spawn wood and once nothing

1 answer

✓ Accepted answer

You dont nescesarily need to add :MoveTo you could just change the position via Explorer or just change it in the script. I used Instance.new() to show you the point. Also If you are doing a 50% chance, you should use a range of 1,2

Here is an example

local random = math.random(1,2)
local debounce = false
bool = true

while bool == true do
	print(random)
	if random == 1 and debounce == false then
		local wud = Instance.new('Part')
		wud.Transparency = .5
		wud.Position = Vector3.new(-88.89, 0.5, 36.11) -- Change Pos
		wud.BrickColor = BrickColor.new('Brown')
		wud.Parent = game.Workspace
		debounce = true
		wait(2)
		debounce = false
	elseif random == 2 and debounce == false then
		print("nothing")
		debounce = true
		wait(2)
		debounce = false
	end
end


Some Error Reasons -

  • The reason why it spawns all at once is because you have no debounce or wait time as it is a infinite loop,

This discussion is preserved as it was. New answers, comments, and votes are closed.