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

How to check if Player is in array?

Asked by
Seyfert 90
7 years ago

I want to make a voting pad where voters can cast a vote to kick the leader. I am making an array and inserting the player's name into the array however the issue I come across are two things

  1. Knowing the difference between an empty entry and an entry that already has another player's name in it.
  2. Making sure the current player's name is not in any index in the array.

Here is my code so far

local voterArray = {""}
local debounce = script.Parent.debounce


function onTouched(hit)
    if debounce.Value == true then
        debounce.Value = false
        for index, value in ipairs(voterArray) do
            print(index, '=', value) 
            if value == "" then --Make sure value in each index is empty, if its not empty its somebody else's player name already in there
                if value ~= hit.Parent then --if a value in an index does not contain the touched player's name then
                    addPlayer(hit.Parent)
                else
                    game.Workspace['Vote to kick Leaderr'].Name = "You already voted"
                    self.BrickColor = BrickColor.new("Bright blue")
                    wait(2)
                    game.Workspace['You already voted'].Name = "Vote to kick Leader"    
                    self.BrickColor = BrickColor.new("Bright red")
                    wait(3)
                end     
            end 
        end
    debounce.Value = true
    end
end

function addPlayer(player)
    local playerName = player
    table.insert(voterArray, playerName)
    print("added player to array")
end



0
What do you mean by "check if a player is in an array?" Are you trying to check if it's an actual player? If you're using the PlayerAdded function or GetPlayers function, it'll always return an actual player. TheeDeathCaster 2368 — 7y
0
No I am checking if a player's name is already inside the array to prevent the same player from voting twice. Seyfert 90 — 7y
0
Then you would compare the player's name to a value in the table, via iterating over the table, or using the brackets thing (forgot name ;-;). ([player.Name] = Value) TheeDeathCaster 2368 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

This should be all the code you'll need for what you're going for. Just add the connection line for the onTouched function and you're good to go. I'm not really sure why you were worried about creating "empty entries", but I digress.

Basically all this does, is upon the player touching whatever object you connect the OnTouched function to, it then checks to see if that player's name is held within the table named voterArray. If the player's name is already in this table, that means the player has already voted, and can not vote again. If the player's name is not already in this table, the addPlayer function is then fired and adds a string matching the player's name, to the table. Test this code out as it is in your place, and let me know if it's what you were trying to achieve. If it is, study it, learn from it. If you have any questions I'd be more than happy to discuss it with you.

If this answer helps you in any way, or solved your issue, upvote and confirm the answer. Thanks

EDIT: SearchTable function has been edited. The function would not iterate fully through the entire table before returning a bool value. This issue has been resolved and the code fully works.

Always remember to study, learn, and understand the code people give you - reverse- engineer it. If you have questions ask, but only copy and pasting never gets anyone anywhere. Use the resources on the Roblox Wiki as well. :)

local voterArray = {}
local db = false

-- The prints are just helpful checks, remove them if you want

function SearchTable(tab, value)
    print(table.concat(voterArray, ", "))
    local bool
    bool = false
    for i,v in ipairs(tab) do
        if v == value then
            bool = true
        end
    end
    return bool
end

function onTouched(hit)
    if db == true then
        return
    end

    db = true

    if hit.Parent:FindFirstChild("Humanoid") then
        local player = hit.Parent
        if SearchTable(voterArray, player.Name) == true then
            print("Player has already voted")

            -- Put code for when player has already voted here
        else
            print("Player's vote is being cast")
            addPlayer(player.Name)      
        end
    end

    wait(1)
    db = false
end

--game.Workspace.Part.Touched:connect(onTouched)    -- connection line

function addPlayer(playerName)
    table.insert(voterArray, playerName)
    print("Added player to array")
end
0
For lines 9-13, are you sure that that's a good idea? Because if the table has a player's name that isn't who the function's argument was given, then it'll return false right on the spot, except if the table is empty. TheeDeathCaster 2368 — 7y
1
Also, please explain the reasoning behind your answer; it doesn't benefit the OP or anyone if they don't understand what was wrong/ what you did to fix it. Even though the answer may be accepted, my point still stands; not everyone will understand what was wrong by just looking at your code, and may accept it because it worked. (Not saying all askers are like this.) TheeDeathCaster 2368 — 7y
0
I was creating empty entries so that i would have a way to differentiate between an empty entry and one that isnt empty, the non empty being another player's name. If I am iterating through all of the indexes in the array and check if the value is not equal to the current player's name I could have multiple of the player's name. This person used SearchTable which works differently I guess. Seyfert 90 — 7y
0
So if I completely skip indexes that are not empty that leaves me only......eh that doesnt really work well at all either. I dont think for loops wkrk well with what I want to do, im hoppimg SearchTable will. Seyfert 90 — 7y
View all comments (8 more)
0
Im dumb i just realized searchtable was a funxtion you made, idk if it will work but i will give it a try. Seyfert 90 — 7y
0
First, test the code I wrote as is, and see if it's what you're looking for. Just add a connection line for the OnTouched function. All this code does is check to see if the player's name is already in the table, if it's not, then the player's name is added. If the name is already in the table, then it prints and does whatever other code you want it to do upon that scenario happening. SteamDemand 312 — 7y
0
@TheeDeathCaster, yes you were correct about lines 9-13. The function would not fully iterate throughout the entire table before returning a bool. Just a little logic mishap. I have edited the code and resolved the issue. Thanks for pointing it out. SteamDemand 312 — 7y
0
Hmm, I tried it but it still appears that the addPlayer(player.Name) gets ran even thouhg the player's name is in the array. Seyfert 90 — 7y
0
Use my new edited code - I changed the SearchTable function. SteamDemand 312 — 7y
0
I....I figured out the problem, I had addPlayer(playerName) inside of the for i v in the SearchTable function....I had left it in there by accident I guess. I also adapted your bool = false at the top already. It seems like it is working now! I find it very interesting the way you used an if statement and return to mitigate around the issue I had, never thought of doing that but now I learned a n Seyfert 90 — 7y
0
I'm glad you learned something and were able to adapt my code into yours! Yes, none of the functions should be inside eachother's scopes. I'm glad you feel it's working! If I helped you at all, upvote my answer and confirm it. Thanks! SteamDemand 312 — 7y
0
\(^-^)/ TheeDeathCaster 2368 — 7y
Ad

Answer this question