Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

2 comments

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.

0

Oh. That was a typo that I made; it's not in the actual code. Thanks for looking though.

1 answer

✓ Accepted answer

: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.

This discussion is preserved as it was. New answers, comments, and votes are closed.