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.
Can someone help me fix it so they only get a singular ball?
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
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.
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.