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

Why is my last one standing script not working when I compare the variable to the array value?

Asked by 4 years ago
Edited by Azarth 4 years ago

Hi, I'm programming a game where the last player standing wins. The server gets an array of all of the player names using

playerArrayTest = game.Players:GetPlayers()

Then, as each player dies, the client sends a remote event to the server telling the server to remove that specific username from the array. When there's one player name in the array left, that person wins.

function removePlayerFromArray(player, playerName)

    --playerName is the username from the client
    --playerArrayTest is the array of usernames         

    print("Incoming client event...")
    print("Server will remove".. " " ..playerName.. " " .."from Server's player array."

    if playerArrayTest[1] == playerName then
        playerArrayTest[1] = nil
    else
        print("Could not locate player.")
    end

    --The if-statements repeat this down to...

    if playerArrayTest[8] == playerName then
        playerArrayTest[8] = nil
    else
        print("Could not locate player.")
    end

    --...because there are a maximum of 8 players in the game at once.
end

The playerName and the playerArrayTest value match when I'm testing it, but for some reason the game doesn't think that they do, which results in the output

--Could not locate player

Is this because you can't compare an array value to a variable, or am I just doing something wrong? If somebody could help or point me to a different last one standing program that you have used, that would be greatly appreciated!

0
PlayerTestArray, then PlayerArrayTest - It seems like a minor mistake you accidentally slipped through. Replace playerTestArray on line 09 and 17 with playerArrayTest, and let's see if it works. Afterl1ght 321 — 4y
0
Oh. That was a typo that I made; it's not in the actual code. Thanks for looking though. blarp_blurp645 63 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited by Azarth 4 years ago

:GetPlayers() returns a table of Player instances, and I assume playerName is a string that represents name of a player. So technically, you're trying to compare Player instance with a string!

You can try adding each name of players in existence in a variable as a table, by then you'll be able to check if player exists in the table using their name.

local playerArrayTest = {}

function updatePlrList()
    --First we clean the table to avoid further unforeseen conflicts.
    playerArrayTest = {}
    --Check each available player in the server.
    for _,p in pairs(game.Players:GetPlayers()) do
        --Add name of player into playerArrayTest as an element
        table.insert(playerArrayTest, p.Name)
     end
end

--Execute this function to update the list.
updatePlrList()

There's also another way which is to use Player instance for playerName instead so they could be compatible.

Also, here's an extra:

--part of your script
if playerArrayTest[1] == playerName then
    playerArrayTest[1] = nil
else
    print("Could not locate player.")
end

--The if-statements repeat this down to...

if playerArrayTest[8] == playerName then
    playerArrayTest[8] = nil
else
    print("Could not locate player.")
end

This is quite cumbersome if you really did copy and paste them. Using for loop, we can flatten it out so the script will be a little bit more comfortable to run.

--Another ver of a part of your script
for i = 1,10,1 do
    if playerArrayTest[i] == playerName
        playerArrayTest [i] = nil
    else
        print("Could not locate player.")
    end
end

I'm on a phone, so all of these are not tested yet. It's midnight rn and I'm sorry if there are any reoccurring problems after this.

Ad

Answer this question