Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Operations with #GetChildren()?

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)

2 comments

0

Why is it a child added event?

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.

1 answer

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.

3 comments

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.

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.

0

Did you try the version I shared here?

This discussion is preserved as it was. New answers, comments, and votes are closed.