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