Big problem server to client!

So this is a script with a loop where the server should store a loop for each player diffrently, but it doesn’t. When the player clicks it sents a message to the server and the server pics a random number and sends it back to the player. But the loops collide with each other for some reason.
Can someone pelase help me with this.

local replicatedStorage = game:GetService("ReplicatedStorage")
local clickEvent = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("ClickEvent")
local startEvent = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("StartEvent")
local endEvent = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("EndEvent")
local luckEvent = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("LuckEvent")
local players = game:GetService("Players")
local debounce = false
local clickDebounce = false

players.PlayerAdded:Connect(function(player)
	while true do
		if debounce == false then
			debounce = true
			
			local luckTable = {}
			local amount = 0

			startEvent:FireClient(player)

			clickEvent.OnServerEvent:Connect(function(player)
				if clickDebounce == false then
					clickDebounce = true
					amount += 1
					if amount < 10 then

						local function getRandomValue(minValue, maxValue, uniformRandomValueModifier)
							local uniformRandomNumber = math.random()
							return minValue + uniformRandomValueModifier(uniformRandomNumber) * (maxValue - minValue)
						end

						local function exampleModifierFunction(uniformRandomNumber)
							return uniformRandomNumber ^ 5 / player.Values.Chance.Value
						end

						-- example usage
						local luckNumber = getRandomValue(1, player.Values.Range.Value, exampleModifierFunction)
						
						luckNumber = math.floor(luckNumber + 0.5)

						table.insert(luckTable, luckNumber)
						luckEvent:FireClient(player,luckNumber,amount)
					else
						amount = 0
						local totalLuck = 0

						for i,v in pairs(luckTable) do
							totalLuck += v
						end

						totalLuck /= 10

						if totalLuck > player.leaderstats.Luck.Value then
							player.leaderstats.Luck.Value = totalLuck
						end

						player.leaderstats.Rolls.Value += 1
						player.Values.TotalLuck.Value += totalLuck

						endEvent:FireClient(player,totalLuck)

						debounce = false

						for i,v in pairs(luckTable) do
							table.clear(luckTable)
						end
					end
					wait(0.5)
					clickDebounce = false
				end
			end)
		end
		wait(1)
	end
end)
3 Likes

Why are you constantly connecting the .OnServerEvent? And why can’t the client generate the random number?

1 Like

You cant trust the client the client will just sent the number 1,000,000 to the server

1 Like

is there something in the output? And what’s on the other side

no theres no error or anything like that the script just needs changing

If you want the server to return a random number to the client, consider using RemoteFunctions.

What exactly does this number? Even if the client gets the number from the server, it can still be interfered with by external scripts. (If thats your reason for sending it from the server.)

And the if the client were to inject scripts, it can still send the number 1,000,000 to the server in another script.