Make this script function if only 70% of the players in the server voted

Hello. Attempting to script a clean debris queue pad, I don’t know how to script the voting part.

local pad = script.Parent
local text = pad.VoteGui.TextLabel
local voted = pad.Voted
local ready = pad.Ready

pad.Touched:Connect(function()
	if ready == true then
		ready = false
		for _, stations in pairs(game.Workspace:GetDescendants()) do
			if stations:IsA("Model") and stations.Name == "Station" then
				for _, vehicles in pairs(stations:GetChildren()) do
					if vehicles:IsA("Model") then
						vehicles:Destroy()
					end
				end
			end
		end
		for i = 60, 0, -1 do
			text.Text = "On Cooldown (" .. i ..")"
			print("Ready to vote")
			ready.Value = true
			wait(1)
		end
		text.Text = "Clean Debris (0/0)"
	end
end)

Here is the properties in explorer:
image

I don’t know how your system is really set up so I can’t actually code it, but assuming you can access the voting GUI and use .MouseButton1Click from the server, you can count votes through using that and reset the votes each round. The voting can be enabled or disabled with ready.Value.

I haven’t set up a system yet, but its supposed to be a queue pad where players have to touch it.

In that case, you can determine how many players are in the area using whatever function it’s called. Probably in workspace. I’d reference the object browser if I were you.

I believe it is GetPartBoundsInBox if I remember correctly. You can additionally make the floor a unique material and check the Humanoid.FloorMaterial property.

To implement voting, you can use RemoteEvents to communicate between the client and server scripts. Keep track of the number of players who have voted and trigger the cleanup process once a certain threshold is reached.

This function?

local playercount = #game.Players:GetPlayers()
local count = 0
pad.Touched:Connect(function()
	count += 1
	if count == playercount * 0.7 then
		-- action
	end
end)

.Touched is not accurate in that regard because characters have multiple parts. You can detect if the name is the humanoid root part, and make a hitbox for it. You’d need to undo the vote when the player leaves using .TouchEnded.

pad.Touched:Connect(function(hit)
	local human = hit.Parent:findFirstChild("Humanoid") 
	if (human ~= nil ) then 
		count += 1
		if count == playercount * 0.7 then
			-- action
		end
	end
end)

This?

I improved the script:

local pad = script.Parent
local text = pad.VoteGui.TextLabel
local voted = pad.Voted
local ready = pad.Ready
local playercount = #game.Players:GetPlayers()

wait(.1)
voted.Value = 0

voted:GetPropertyChangedSignal("Value"):Connect(function()
	text.Text = "Clean Debris (" .. voted.Value .. "/" .. playercount .. ")"
end)

pad.Touched:Connect(function(hit)
	local human = hit.Parent:findFirstChild("Humanoid") 
	if (human ~= nil ) then 
		voted.Value = voted.Value + 1
		print("Player has voted")
		if voted == playercount * 0.7 then
			voted.Value = 0
			for _, vehicles in pairs(game.Workspace:GetDescendants()) do
				if vehicles:IsA("Model") and vehicles.Parent.Name == "Station" then
					vehicles:Destroy()
				end
			end
			print("Sucessfully cleaned up")
		end
	end
end)

But the pad acts like I didn’t vote before:
image

My mistake. Your touch pad is a pad and not a hitbox. Assuming you don’t want the player to be able to vote again, this should also work.

With that being said, your current code will cause multiple votes allowed by the same person because you’re not checking if they’ve already voted. This can be fixed with a table.

--put in the top of the script
local voters = {}
--after if (human ~= nil) then, put
if not table.find(voters, human) then
table.insert(voters, human)
else
return
end
--When the round ends, or if votes are completed, put
table.clear(voters)

Unless I’m not understanding how your game design works, this should work just fine. One thing to note though is that if the player resets their character, they will be able to vote again.

The method works, but I somehow need to get the 70% of the server…
image

local playercount = #game.Players:GetPlayers()

does not work, somehow…

Interesting. Maybe it has problems if there’s only one player?

so instead of doing 0, (thats undefined)
do this instead

this is if you are saying that if 70% or more players vote yes

It works, but does not display the amount of players needed to vote.
Here is the code for it:

voted:GetPropertyChangedSignal("Value"):Connect(function()
	text.Text = "Clean Debris (" .. voted.Value .. "/" .. playercount * .7 .. ")"
end)

You might want to make that:

text.Text = "Clean Debris (" .. voted.Value .. "/" .. math.ceil(playercount * .7) .. ")"

so that it rounds up the number.

Rounding up the number does not do anything…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.