Need a hand with getting script to give point to winner useing (activeplayers 1)

Hi so ive been playing about with this but can seem to get my finger on it, how would i get the script to give a point to the winner

i have leaderstats set .leaderstats.wins.value

thanks for your time your all yodas to me


-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer.leaderstats.Wins.Value
-- Modules
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))

-- Events
local events = ServerStorage:WaitForChild("Events")
local matchEnd = events:WaitForChild("MatchEnd")

-- Map Variables
local lobbySpawn = workspace.Lobby.StartSpawn
local arenaMap = workspace.Arena
local spawnLocations = arenaMap.SpawnLocations




-- Values
local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
local playersLeft = displayValues:WaitForChild("PlayersLeft")
local Players = game:GetService("Players")
-- Player Variables
local activePlayers = {}


local function onPlayerAdded(player)

	local leaderstats = Instance.new("Folder")

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player


	local Wins = Instance.new("IntValue")

	Wins.Name = "Wins"

	Wins.Value = 0

	Wins.Parent = leaderstats

end

Players.PlayerAdded:Connect(onPlayerAdded)



local function checkPlayerCount()
	if #activePlayers == 1 then
		matchEnd:Fire(gameSettings.endStates.FoundWinner)
		
		print("Found winner")
	end
end

local function removeActivePlayer(player)
	print("removing player")
	for playerKey, whichPlayer in pairs(activePlayers) do
		if whichPlayer == player then
			table.remove(activePlayers, playerKey)
			playersLeft.Value = #activePlayers
			checkPlayerCount()
		end
	end
end

local function respawnPlayerInLobby(player)
	player.RespawnLocation = lobbySpawn
	player:LoadCharacter()
end

local function preparePlayer(player, whichSpawn)
	player.RespawnLocation = whichSpawn
	player:LoadCharacter()

	local character = player.Character or player.CharacterAdded:Wait()
	-- Give the player a tool
	

	local humanoid = character:WaitForChild("Humanoid")

	humanoid.Died:Connect(function()
		respawnPlayerInLobby(player)
		removeActivePlayer(player)
	end)
end

local function onPlayerJoin(player)
	player.RespawnLocation = lobbySpawn
end



function PlayerManager.sendPlayersToMatch()
	local availableSpawnPoints = spawnLocations:GetChildren()

	for playerKey, whichPlayer in pairs(Players:GetPlayers()) do
		table.insert(activePlayers,whichPlayer)

		-- Gets a spawn location and then removes it from the table so the next player gets the next spawn
		local spawnLocation = availableSpawnPoints[1]
		table.remove(availableSpawnPoints, 1)
		preparePlayer(whichPlayer, spawnLocation)
	end

	playersLeft.Value = #activePlayers
end

function PlayerManager.getWinnerName()
	if activePlayers[1] then
		
		local winningPlayer = activePlayers[1]

		
		 Wins.Value = Wins.Value + 1
		return winningPlayer.Name
		
		
	else
		return "Error: No player found"
	end
end


function PlayerManager.resetPlayers()
	for playerKey, whichPlayer in pairs(activePlayers) do
		respawnPlayerInLobby(whichPlayer)
	end

	activePlayers = {}
end

-- Events
Players.PlayerAdded:Connect(onPlayerJoin)

return PlayerManager ```

To give a point to the winner, you can modify the PlayerManager.getWinnerName() function to update the “Wins” value of the winning player:

function PlayerManager.getWinnerName()
    if activePlayers[1] then
        local winningPlayer = activePlayers[1]
        winningPlayer.leaderstats.Wins.Value = winningPlayer.leaderstats.Wins.Value + 1
        return winningPlayer.Name
    else
        return "Error: No player found"
    end
end

This code retrieves the first player in the activePlayers table (assuming they are the winner) and then increments their “Wins” value by 1.

1 Like

thats working thanks for taking the time to get back to me im just starting to learn all this
image

1 Like

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