Math.random issue with Spike Script

Hey.

Recently I made a post regarding this, and it worked!

I have one more major bug with this though…

You may think: but huh? it works!

Yes. It does work, but it doens’t math.random correctly.

local RandomNumber = {8, 9, 10, 11}

If you can fix my script, thanks.

--Variables
local RandomizedSpikes = game.Workspace.MyGame.DescendantsMap.RandomizedSpikes:GetChildren()
local RandomNumber = {8, 9, 10, 11}

local debounce = true

--Executor
while true do
	
	local randomNumber = math.random(1, #RandomNumber)
	local changed = {}

	for i = 1, randomNumber do
		local id = math.random(randomNumber,#RandomizedSpikes)
		RandomizedSpikes[id].BrickColor = BrickColor.new("Neon orange")
		table.insert(changed,RandomizedSpikes[id])
		wait(0.5)
		RandomizedSpikes[id].SpikesPart.Transparency = 0
		
		RandomizedSpikes[id].Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and RandomizedSpikes[id].SpikesPart.Transparency == 0 then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if debounce == true then
					debounce = false
					player.Character.Humanoid.Health -= 50
					wait(0.5)
					debounce = true
				end
			end
		end)
	end

	wait(3)

	for _ , v in pairs(changed) do
		v.BrickColor = BrickColor.new("Medium stone grey")
		v.SpikesPart.Transparency = 1
	end

	changed = {}

	wait(1)
end

Thanks.

1 Like

Could be because of this. You aren’t taking any of the values from the Table randomNumber but rather getting the Number (#) of instances in the table. (Which is causing it to return 4)

2 Likes