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

Why does this get stuck at the generic for iterating through a table?

Asked by
Freemium 110
10 years ago

Whoever has answered/saw my questions, here is the reason for all of those. I'm making a voting gui that sees if players think the server is broken. When they vote yes/no, their name is added to the table so they cannot vote again. What is wrong with my script? There's no output errors.

voted = {}
plr = script.Parent.Parent.Parent.Parent

script.Parent.NoBttn.MouseButton1Click:connect(function()
for x, e in pairs(voted) do
    if not plr.Name == e then
        table.insert(voted, 1, plr.Name)
for b = 1,1 do
Val2.Value = Val2.Value + 1
end
    end
    end
end)

It seems to be getting stuck at the generic for.

2 answers

Log in to vote
1
Answered by
AxeOfMen 434 Moderation Voter
10 years ago
local voted = {}
local plr = script.Parent.Parent.Parent.Parent
script.Parent.NoBttn.MouseButton1Click:connect(function()
    if not voted[plr.Name] then
        voted[plr.Name] = true
        Val2.Value = Val2.Value + 1
    end
end)

Give this a try.

It looks like this is in a LocalScript in which case this could be implemented much more simply. However, if it is in a LocalScript, it will be recreated with the player when they respawn, allowing the player to vote again.

Here is a more robust method of counting votes and disallowing multiple votes from the same player:

Create a Script object in ServerScriptService and put this code in it:

local voted = {}
local playerVote = Instance.new("RemoteFunction")
playerVote.Name = "PlayerVote"
playerVote.OnServerInvoke = function(player, vote)
    if not voted[player] then
        voted[player] = true
        if vote then
            --Add a "Yes" vote
            --TODO - Add your code here for counting a "Yes" vote. Maybe increment the value of Val1?
        else
            --Add a "No" vote
            Val2.Value = Val2.Value + 1
        end
    end
end)
playerVote.Parent = workspace

Create a LocalScript object as a sibling to the Yes and No buttons containing this code:

local playerVote = workspace:WaitForChild("PlayerVote")
--connect "No" button to handler
script.Parent.NoBttn.MouseButton1Click:connect(function()
    playerVote:InvokeServer(false)
end)
--connect "Yes" button to handler
script.Parent.YesBttn.MouseButton1Click:connect(function()
    playerVote:InvokeServer(true)
end)
0
It's not in a local script, it's just in a script. Should I still use this method? Freemium 110 — 10y
0
Yes, use the more robust method. If you are using a Script object inside StarterGui it too will be recreated when the player respawns. I added some clarification about where to put the Script and the LocalScript. AxeOfMen 434 — 10y
0
In the script that I have to put in ServerScriptService, the end) is underlined in red so I just changed it to end. Is that OK? Freemium 110 — 10y
0
It's working, thank you so much! Sorry for responding so late. Freemium 110 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago

I might not be correct, but I am pretty sure that since there is no one in the table it cannot do a loop. Also since 1 is already 1 it cannot make the value change. Though I may be wrong. For the second for loop I would suggest:

for b=0,1 do
Val2.Value=Val2.Value+1
end

and I would add a name in the table. Not a real name but one that you could remove after a player votes or just say if the name~="guesty" then ...

0
Didn't work. You can ignore the numeric for, that works fine. Freemium 110 — 10y

Answer this question