Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

how would i get the players name without it being blank? (server script)

Asked by 4 years ago
Edited 4 years ago

so i have a function with returns a string for a message to be put out when the game ends but i just dont know how to get the username of the player. (thing with elseif gamemode==2 then)

function (the lines i wanna focus on are 22-28)

function getWinningTeamString()
    local red = 0
    local blue = 0

    local players = game.Players:GetChildren()
    local a = 0

if gamemode == 1 then
    for i=1,#players do
        if (players[i].TeamColor == game.Teams:FindFirstChild("BLUE TEAM").TeamColor) then
            if (players[i]:FindFirstChild("leaderstats") and players[i].leaderstats:FindFirstChild("Kills")) then
                blue = blue + players[i].leaderstats:FindFirstChild("Kills").Value
            end
        end

        if (players[i].TeamColor == game.Teams:FindFirstChild("RED TEAM").TeamColor) then
            if (players[i]:FindFirstChild("leaderstats") and players[i].leaderstats:FindFirstChild("Kills")) then
                red = red + players[i].leaderstats:FindFirstChild("Kills").Value
            end
        end
    end
    elseif gamemode==2 then
    for i=1,#players do
        if (players[i]:FindFirstChild("leaderstats") and players[i].leaderstats:FindFirstChild("Kills")) then

        end
    end
end


    if gamemode==1 and (red == blue) then return "Stalemate! (Nobody won...)" end
    if gamemode==1 and (red > blue) then return string.format("Red defeated the opposition by %d points!", red - blue) end
    if gamemode==1 and (red < blue) then return string.format("Blue defeated the opposition by %d points!", blue - red) end

    if gamemode==2 then
            return "The winner is "..value1(username).." with "..value2(kills).." kills!"
    end 

    end

where this function is used is here (line 14):

while true do
    players = game:GetService("Players"):GetChildren()
    repeat wait(1) do end until #players>=2
        gamemode = 2--math.random(1,2)
    if gamemode==1 then
        gamestart(mapstorage.TDM:GetChildren())
    elseif gamemode==2 then
        gamestart(mapstorage.FFA:GetChildren())
    end
    wait(game_length-30)
        messageEvent:FireAllClients("30 seconds left until the end of this round!",5)
        script["30seconds"]:Play()
    wait(30)
        messageEvent:FireAllClients(getWinningTeamString(),30) -- right here btw 
-- i cut off the rest since theres no point in displaying it

1 answer

Log in to vote
1
Answered by 4 years ago

What you need to do is index the player in your for loop, and then index its properties.

local Players = game:GetService("Players"):GetPlayers()

for i = 1, #Players do 
    local plr = Players[i]
    local leaderstats = plr:FindFirstChild("leaderstats")
    if leaderstats then 
        local kills = leaderstats:FindFirstChild("Kills")
    end
end

All you will need to do to index the player's name is plr.Name.

0
thanks bro speedyfox66 237 — 4y
Ad

Answer this question