Round script ALWAYS making 1st player to join win

Hello, could someone help me to fix this issue with my round handler?

  1. What do you want to achieve? This part of the script is intended to find the player who won and how many points they had,

  2. What is the issue? The winner is ALWAYS being set to the person who joined first, regardless of points.

  3. What solutions have you tried so far? I tested with my friend and determined that there is an issue with highestnumber. I am unsure if the tables are being set wrong or if the variables from the tables are being read wrong.

Relevant code:

    local timer = game.Workspace.TimerSign.SurfaceGui.TimerLabel
    local leaderboard = game.Workspace.Leaderboard.SurfaceGui.TextLabel
    
    local players = game:GetService("Players")
    local storage = game.ReplicatedStorage
    local servstorage = game.ServerStorage

--The above is from the start of the code

	--Tables
	local playertable = {}
	local player_results = {}
	
	--Make functions
	local function getResults()
		for i, v_player in ipairs(players:GetPlayers()) do
			playertable[i] = v_player.Name
			player_results[i] = v_player.leaderstats.Points.Value
			v_player.leaderstats.Points.Value = 0
		end
	end
	
	local function calculateResults()
		local highestnumber = -1 --Set highest number

		for i, v in pairs(player_results) do --Find index of highest number in results

			if v > highestnumber then
				highestnumber = v
			end

			local indexfound = table.find(player_results, highestnumber)

			return indexfound
		end
	end
	
	--IndexFound returning 1 always? (highestnumber not setting properly)
	
	--Run functions
	
	getResults()
	
	local indexfound = calculateResults() --Get indexfound
	local winningplayer = playertable[indexfound] --Use indexfound to find winning player
	local highestnumber = player_results[indexfound] --Use indexfound to find highest score
	
	leaderboard.Text = "#".. tostring(indexfound) ..", ".. tostring(highestnumber)
	
	
	players[winningplayer].leaderstats.Wins.Value += 1
	
	storage.ShowResults:FireAllClients(winningplayer, highestnumber)
	
	local playertable = {}
	local player_results = {}

Thank you for looking at this issue!

The for loop in calculateResults() will always return on the first loop. indexfound and the return statement need to be outside the loop.

1 Like

Would it look like this?

1 Like

That looks good, test it and see

Could u join me in roblox to test? My username is hfhfvvheh (wait u can see that lol)

You should be able to test this yourself in studio by going to “Test” up the top then starting a test with 2 or more simulated players.
image

Ty, do u know if I could set the 2nd player to have a certain leaderstat?

When you’re testing studio will open multiple testing windows, you should go to the one that is the server and not one of the testing players, then go into Players > [player name] > leaderstats and manually change the value you want to.

1 Like

This is causing a massive lagspike for me lol

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