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

Why doesn't math.random produce random results?

I'm trying to get thunder bolts to pop up in random positions when you click. But I'm having some problems.

If I clone the thunder bolt then it won't show up at all. And if I don't then it always goes to the exact same position every time. Here's my code

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, isTyping)
	
	if isTyping then
		return
			
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("Clicked")
		
		local SpawnPoints = {UDim2.new({0.029, 0},{0.016, 0}),
		UDim2.new({0.844, 0},{0.756, 0}),
		UDim2.new({0.91, 0},{0.046, 0}),
		UDim2.new({0.757, 0},{0.136, 0}),
		UDim2.new({0.56, 0},{0.797, 0}),
		UDim2.new({0.56, 0},{0.505, 0}),
		UDim2.new({0.705, 0},{0.44, 0}),
		UDim2.new({0.599, 0},{0.009, 0}),
		UDim2.new({0.398, 0},{0.108, 0}),
		UDim2.new({-0.004, 0},{0.312, 0}),
		UDim2.new({0.529, 0},{0.222, 0}),
		UDim2.new({0.418, 0},{0.637, 0}),
		UDim2.new({0.174, 0},{0.748, 0}),
		UDim2.new({0.024, 0},{0.65, 0}),
		UDim2.new({0.315, 0},{0.34, 0}),
		UDim2.new({0.226, 0},{0.555, 0})}
		
		local Bolt = script.Parent.Bolt:Clone()
		Bolt.Visible = true
		Bolt.Position = SpawnPoints[math.random(1, #SpawnPoints)]
		
		
	end
end)

1 comment

0

Why aren't you setting the clone's parent after cloning it?

1 answer

✓ Accepted answer

All of the UDim2s that you constructed are at 0, 0, 0, 0 because Roblox does not throw error when you put different data type into the constructor, if you don't see the problem, UDim2.new takes 4 numbers as a parameter, not 2 tables of numbers.

local SpawnPoints = {
	UDim2.new(0.029, 0, 0.016, 0),
	UDim2.new(0.844, 0, 0.756, 0),
	UDim2.new(0.91, 0, 0.046, 0),
	UDim2.new(0.757, 0, 0.136, 0),
	UDim2.new(0.56, 0, 0.797, 0),
	UDim2.new(0.56, 0, 0.505, 0),
	UDim2.new(0.705, 0, 0.44, 0),
	UDim2.new(0.599, 0, 0.009, 0),
	UDim2.new(0.398, 0, 0.108, 0),
	UDim2.new(-0.004, 0, 0.312, 0),
	UDim2.new(0.529, 0, 0.222, 0),
	UDim2.new(0.418, 0, 0.637, 0),
	UDim2.new(0.174, 0, 0.748, 0),
	UDim2.new(0.024, 0, 0.65, 0),
	UDim2.new(0.315, 0, 0.34, 0),
	UDim2.new(0.226, 0, 0.555, 0)
}

Shoulf fix it. You also should be declaring the SpawnPoints variable outside of the function since there is no point in creating new table like this every time you press some input.

2 comments

0

Thanks a lot

0

Thanks a lot

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