Roblox工作室 – 如何将拥有最多分数的玩家设为胜利者?

huangapple go评论53阅读模式
英文:

Roblox studio - How to give the player with most points a win?

问题

我正在制作一个轮次类型的游戏,玩家通过正确回答问题来获得积分。

我想在轮次结束时将最多积分的玩家视为获胜者,但我不知道如何做到这一点。我已经在leaderstats中准备了NumberValues。这是我完成游戏的最后一步。

有人能给我建议吗?谢谢。

英文:

I'm making a round-type game, where players are getting points for a correct answer.

I'll like to give the player with most points (at the end of the round) a win, but I have no idea, how to do that. I have prepared both NumberValues in leaderstats. This is literally the last thing that separates me from finishing my game.

Can someone advise me? Thanks.

答案1

得分: 1

你可以开始编写一个服务器脚本,等待游戏/回合结束,收集所有玩家的 leaderstats NumberValues 并进行比较。

如果你对如何比较多个玩家的值感到困扰,可以通过迭代玩家并将任何高于前一个值的值存储到一个带有玩家名字或ID的变量中来开始。个人建议使用 For Loop 来做这个,循环结束后,你可以检查谁赢了,并给予该玩家一场胜利。以下是我编写代码的示例:

local players = game:GetService("Players")

local function endRound()
	
    -- 用于记录每轮赢家的数组
    local winner = {nil, 0}
	
    -- 迭代玩家并比较分数
    for i, player in pairs(players:GetPlayers()) do
        if player.leaderstats.Points.Value > winner[2] then
            winner = {player, player.leaderstats.Points.Value}
        end
    end
	
    -- 将赢家的胜利次数加1
    winner[1].leaderstats.Wins.Value = winner[1].leaderstats.Wins.Value + 1
	
end

wait(60)

endRound()

希望这能帮到你。我进行了一些快速测试以确保代码按预期工作,但它可能还不完美。如果你有其他问题,请留下评论并尝试向我解释你需要帮助的内容。

我在写这个的时候意识到,如果你使用这段代码,你需要确保在平局时最高分由多个玩家共享或者当所有玩家分数都为0时,有相应的处理,以确保脚本在游戏过程中不会中断。

英文:

You could start by having a server script that waits for the game/round to finish that gathers all of the leaderstats NumberValues and compares them.

If you're stuck on how you could compare multiple players values, you could start by iterating through the players and storing any value that is higher than the previous one into a variable with the players name or ID. Personally I would use a For Loop to do this, after the loop is done I'd then check who won and give that player a win. Here is an example of how I would go about coding that:

local players = game:GetService("Players")

local function endRound()

    -- Array that keeps track of winner for each round
    local winner = {nil, 0}

    -- Iterates through the players and compares points
    for i, player in players:GetPlayers() do
	    if player.leaderstats.Points.Value > winner[2] then
		    winner = {player, player.leaderstats.Points.Value}
	    end
    end

    -- Adds 1 win to the wins of the Winner
    winner[1].leaderstats.Wins.Value = winner[1].leaderstats.Wins.Value + 1

end

wait(60)

endRound()

I hope this helps, I did a few quick tests to make sure the code works as intended but it is probably far from perfect. I hope it gives you a rough idea of what to do if it doesn't answer the your question drop a comment and try to explain to me what you would like help with.

Something I realised while writing this is that if you did use that code you would need to make sure you had catches for when the highest points is shared by multiple players in a draw or when players all have 0 points to ensure the script does not break during gameplay.

huangapple
  • 本文由 发表于 2023年5月21日 01:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定