Roulette won't land on reward

So I’m creating a roulette basing off this post. How can I create a UI Spinning Wheel? - #3 by OnlyRoids

I made it with a Weighted Chance System using Trulineo’s post Weighted Chance System

I basically combined the two together and made a roulette.
Screenshot_140

The local script for the roulette looks like this:

spin.MouseButton1Click:Connect(function()
	if debounce == false then
		debounce = true
		game:GetService("ReplicatedStorage").Spin:FireServer()
	end
	
end)

game:GetService("ReplicatedStorage").Spin.OnClientEvent:Connect(function(endrotation)
	local tween = TweenService:Create(wheel, TweenInfo.new(7, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Rotation = endrotation})
	wheel.Rotation = 0
	tween:Play()
	task.wait(7.2)
	debounce = false
end)

And plays perfectly fine but the wheel spins somewhere totally different to where its supposed to land.
The table looks like this.

rewardModule.spinChance = {
	["1Nothing"] = 87.1;
	["2Extra1Spin"] = 8.5;
	["3Extra3Spin"] = 2.5;
	["4Extra5Spin"] = 1.5;
	["5Coins"] = .4;
}

and the ModuleScript for the spinning system looks like this:

local weight = 0
	
	for _, chance in pairs(rewardModule.spinChance) do
		weight += (chance * 10)
	end

	local runNumber = math.random(1, weight)
	task.wait()
	weight = 0
	
	for prize, chance in pairs(rewardModule.spinChance) do
		weight += (chance * 10)
		if weight >= runNumber then
			print(plr.Name.." got "..prize)
			return prize
		end
	end

I ran the function from the ModuleScript through a ServerScript and sent the information on where it should land to the client.
The part of the code where it sends the info looks like this.

local prize = rewardModule.Spin(plr)
--Full spins is 4 and DegreeToSpin is 360/5 = 72
local endRotation = -((360 * -fullspins) + (degreetospin) * (#prize - 1))
rp:FindFirstChild("Spin"):FireClient(plr, endRotation)
5 Likes

Bump cause I really need a fix. been stuck for 2 days

1 Like

Well, here’s what you need to realize: every segment spans 360/5 degrees. That’s 72 degrees. Depending on what the result is, you should change the amount it rotates. first, calculate the offset. This is the index times the 72 degrees. Nothing = 0, ticket UGC = 1, 1 spin = 2, etc.
Then, from that offset, you must always rotate 360 degrees. So if the result is Ticket UGC (1x72), then you may only rotate to 72, 432, 792, etc. If the result is the 3rd option, then you do 3x72, and start counting from there.

This’ll rotate it to exactly in the center every time, but you have 72/2 degrees in either direction, so you should add or substract a random value to the end rotation, so it doesn’t stop in the middle.

2 Likes

I tried printing the index using #prize and it somehow prints 8, 10 and other values. not 1 - 5. Is there something im doing wrong here?

1 Like

Well if you’re printing #prize, then you’re probably printing the length of the string or something.

Because you have a dictionary instead of an array, you can’t count the indices (as they are not ordered). You should make a second array, which is spinOrder, and it’s the order of the keys.

spinOrder = {
"1Nothing", "2Extra1Spin", "3Extra3Spin" ...}

local index = table.find(spinOrder, prize)
2 Likes

Thank you so much! I was really confused since I’m new to tables but this really helped me out!

1 Like

There’s 2 kinds of tables. Arrays, and dictionaries. Arrays count up an index, 1,2,3… whereas Dictionaries have keys that map to a value.

You can iterate over arrays, but not over dictionaries (also known as hashmaps), because the keys aren’t in any specific order. That’s also the difference between in pairs and in ipairs, btw, in ipairs can only be used for an array, and is faster than in pairs.

Also, a lot of table methods won’t work on dictionaries. Like table.insert(), because that tries to append a value to the end of the table, but because a dictionary isn’t ordered, that’s impossible.

2 Likes

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