Randomized Ball - Glitch

Hello Robloxians!

I am working on a game that is going to release very very soon, but I am having a slight issue. When I open a machine and then it fires it (which works) its supposed to get a singular ball and then print out what ball they received.

Instead of getting the singular ball they should have gotten, it gives them multiple balls.

image

Can someone help me fix it so they only get a singular ball?

Chances and Tables

local Ball_Chances = {
	["Overworld"] = {
		["Machine 1"] = {
			["Lavender Dodgeball"] = 50,
			["Bright Blue Dodgeball"] = 25,
			["Toothpaste Dodgeball"] = 20,
			["Bright Green Dodgeball"] = 15,

		},
		["Machine 2"] = {
			["Lime Green Dodgeball"] = 10,
			["New Yeller Dodgeball"] = 5,
			["Neon Orange Dodgeball"] = 2.5,
			["Persimmon Dodgeball"] = 1
		}
	}
}

Actual Function

local function openMachine(player, machine, cost)
	if machine == 1 then
		local availableBalls = Ball_Chances["Overworld"]["Machine 1"]
		local totalChances = 0

		for _, chance in pairs(availableBalls) do
			totalChances = totalChances + chance
		end

		local randomNumber = math.random(1, totalChances)
		local selectedBall = nil

		for ballName, chance in pairs(availableBalls) do
			randomNumber = randomNumber - chance
			if randomNumber <= 0 then
				selectedBall = ballName
				break
			end
		end

		if selectedBall then
			warn("Caught ball:", selectedBall)
			alterPlayerData(player.UserId, "Currency", {["Currency"] = "Cash", ["Sign"] = "-", ["Amount"] = tonumber(cost)})

			print("You caught a", selectedBall, "ball!")
		else
			print("No ball caught")
		end
	end
end

Thanks :smile:

Hello :wave:,

maybe it sounds stupid, but maybe try using a debounce on your function. Perhaps you are firing this function multiple times.

Hope this helps!

Are there any more ways that could work?

What do you exactly mean by that?

Is there a way we can make it like a limit 1 and if you get more then 1 it deletes it instantly and then the remaining prints?

You’re better off just cutting the issue by force than mitigating it,

by not preventing multiple from being shot out, the event itself can have the possibility of being abused for lagging your game and such.

Also, the prints are a log type of thing, it just tells you it occured as it occured. Theres no “removing” the prints.

Yeah, how can we fix it tho?

Thirthcharacterlimit

Debounce seems to be the right call though as you’re not showing how your code is fired, its hard to really pinpoint the reason why its firing alot.

I use a remote event that when fired to the server it works. I currently don’t have access to my computer, I will send you a message when I do though.

You need to go back to what fires the openMachine function. Is it a touched event? Touched events will fire multiple times when a player touches a Part.

If it isn’t a Touched event then figure out why the function is being fired multiple times. As @ConstructedDamage said, a debounce time or value might help you out.

game.ReplicatedStorage.Remotes.PurchaseBall.OnServerEvent:Connect(function(player, machine, cost)
	openMachine(player, machine, cost)
end)

thats the code i use for it.

If Remotes.PurchaseBall is what is calling the openMachine function then you need to figure out why Remotes.PurchaseBall is firing multiple times.

1 Like

Fixed, thanks!

||ThirtyCharacterLimit||

off topic but you should probably have the cost stored on the server rather than getting it from the event to prevent it potentially being abused by the client

Yeah, we have it also in the server to check, just let the player to do it to so they think they are winning. I am 100% recking them in the code from the server.

1 Like

@BeholdTheTerrible on a complete unrelated note, I’m receiving a bug on one of the machines. Can you help? My discord user is papa1v

sorry for the late reply, are you still having this issue? if so, I’ll try to see what’s up when i get can get back to my computer

nope. were all good :slight_smile: thanks for askin

1 Like

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