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

Operations with #GetChildren()?

Asked by 10 years ago

Here's a question, can I perform operations with number of values returned by a table? My thought is to make a simple voting system that checks the number of votes against the number of players until it reaches a 2/3 majority. For some reason though this checking script causes the entire place to lock up when it runs. I think it's to do with dividing by the #pls number.

function update(child)
    local votes = script:WaitForChild("Votes").Value
    if child.Name == "Votes" then return 
    else
        votes = votes + 1
    end

    local pls = game.Players:GetChildren() -- pt. 2 of the problem

    if (votes / #pls) > 0.6 then -- I think this line is the problem
        local new = game.ServerStorage.Ball:Clone()
        if game.Workspace:FindFirstChild("Ball") then
            game.Workspace.Ball:Destroy()
        end
        new.Parent = game.Workspace
        new.Position = Vector3.new(-49, 17.4, 27.034)

        script:ClearAllChildren()
        wait()

        local vt = Instance.new("IntValue")
        vt.Name = "Votes"
        vt.Parent = script
    end
end

script.ChildAdded:connect(update)
0
Why is it a child added event? GoldenPhysics 474 — 10y
0
Oh thanks, because the Vote Caster script creates a Value named after the player, which is parented then to the script. It was the easiest way I could think of this evening. deaththerapy 60 — 10y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Of course you can!

The problem is the way you are counting the vote total, I think. After getting any single vote, you never actually increment the total. Save votes back out to script.Votes.Value.

But eliminate the "Votes" object, really. Just use a variable! It's much cleaner and simpler.

local votes = 0;

function update(child)
    votes = votes + 1
    local pls = game.Players:GetChildren() -- pt. 2 of the problem
    if (votes / #pls) > 0.6 then -- I think this line is the problem
        local new = game.ServerStorage.Ball:Clone()
        if game.Workspace:FindFirstChild("Ball") then
            game.Workspace.Ball:Destroy()
        end
        new.Parent = game.Workspace
        new.Position = Vector3.new(-49, 17.4, 27.034)
        script:ClearAllChildren()
        wait()
        votes = 0;
    end
end

script.ChildAdded:connect(update)

You really should probably find something better than ChildAdded, but that works.

0
Yeah, I originally had two GetChildren() variables to keep it simple, and then tried the Votes Value to get rid of the second one. I'm mainly using ChildAdded so that I can check if the player has already voted on the Casting end. deaththerapy 60 — 10y
0
And no, you can take the Votes object out (just did actually) and the problem still exists. The object was actually supposed to be the solution TO the problem, it isn't the (original) cause. deaththerapy 60 — 10y
0
Did you try the version I shared here? BlueTaslem 18071 — 10y
Ad

Answer this question