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

Why cant you ask if something is not inside of a table.. im frustrated?

Asked by 5 years ago

Im frustrated because I don't know why you cant say "if not TABLE[hit.Parent.Name] then"

I dont know how to say it.. its in a server script for all you people who ask


--||VotePad local Config = script.Parent.Parent.Config local VotesGUI = script.Parent.Parent.ConnectedBoard.VotesGui local VoteDisplay = VotesGUI.VoteDisplay local VoteImage = VotesGUI.Image local DeniedPlayers = {} local debounce = true script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then if not DeniedPlayers[hit.Parent.Name] then -- why does this not work if not debounce then return end debounce = false Config.VotesAmount.Value = Config.VotesAmount.Value + 1 script.Parent.Color = Color3.fromRGB(0,255,0) table.insert(DeniedPlayers, hit.Parent.Name) print(DeniedPlayers) wait(1) debounce = true end end end)
0
you can do that...that returns false if hit.Parent.Name is in the table theking48989987 2147 — 5y
0
If I helped you out, please accept my answer. User#25115 0 — 5y
0
If not, comment under it and tell me what I can fix to help you better. User#25115 0 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

You cannot do that with numeric indices. However, you can do that with string indices. A simple example:

local array = {}
array["hello"] = true
print(not array["hello"])

Side note for future reference:

local array = {}
array["hello"] = true
-- is the same in Lua as
array.hello = true

Putting this all together, we can create a debounce with player names as the indices. I hope this helps. Have a great day scripting!

Edit with example:

local players = game:GetService("Players") -- the recommended way to get any service, including players
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = players:GetPlayerFromCharacter(hit.Parent) -- a useful method to get the player
        if player then
            local specialKey = tostring(player.UserId) -- I prefer the userid
            if not playerDebounceList[specialKey] then -- checking for debounce
                playerDebounceList[specialKey] = true -- placing them in the table for debounce
                -- do your code here
            else    
                wait(1) -- waiting and then removing them from the list
                playerDebounceList[specialKey] = nil
            end
        end
    end
end

The main issue with your code is that table.insert places the item at a numeric index at the end of the table if no index is specified. Also, when you did your checking, you were checking for the player's name as an index when it was really a value in the table.

0
That wouldn't be an array, it would be a dictionary. User#19524 175 — 5y
0
whats the difference? TheluaBanana 946 — 5y
0
dictionary[key] = value; array[index] = value User#19524 175 — 5y
0
Lua supports string indices. That is really a table, but I called it array because table is a keyword in Lua. User#25115 0 — 5y
Ad

Answer this question