Detecting a ui press

How would I change a message to a ui click?

			if msg == "yes" then -- Thing i'm trying to change to a ui click
				print("what")
				if plrsVoted[plr] ~= "yes" then
					
					votesForKick = votesForKick + 1
				end
				plrsVoted[plr] = "yes"
				
			elseif msg == "no" then -- Thing i'm trying to change to a ui click
				print("what")
				
				if plrsVoted[plr] == "yes" then
					
					votesForKick = votesForKick - 1
				end
				plrsVoted[plr] = "no"
			end		
		end	
	end)
end)

Are you able to use two gui text buttons to work as yes and no? Here’s how I would implement it using two buttons named “yes” and “no” (with their text saying the respective word), which are siblings to a localscript parented to a frame, containing the following:

script.Parent.yes.Activated:connect(function()
	print("yes")
	votesForKick += 1
	plrsVoted[plr] = "yes"
end)

script.Parent.no.Activated:connect(function()
	print("no")
	votesForKick -= 1
	plrsVoted[plr] = "no"
end)

Alternatively, if this needs to be done on the server (since it looks like there’s a table of voters) you’d use the same button setup and have it fire a boolean at the server using a remote event.

You should not have to do that

And can you write the full code?

It was named as such to explain the example code, it could be named anything.

local votekickCmd = "!votekick"

local cooldown = 60
local plrsOnCooldown = {}

local timeForVote = 10

local isVotingForKick = false
local plrsVoted = {}

local votesForKick = 0

local systemMsgRE = game.ReplicatedStorage["Other Remotes"]:WaitForChild("Votekick")


game.Players.PlayerAdded:Connect(function(plr)
	
	plr.Chatted:Connect(function(msg)
		
		if string.sub(msg, 1, string.len(votekickCmd)) == votekickCmd and not isVotingForKick then
			
			local plrVoted = game.Players:FindFirstChild(string.sub(msg, string.len(votekickCmd) + 2))
			
			if plrVoted and not plrsOnCooldown[plr] then
				
				plrsOnCooldown[plr] = true
				
				isVotingForKick = true
				votesForKick = 0
				
				systemMsgRE:FireAllClients(plrVoted, plr)
				
				wait(timeForVote)
				
				isVotingForKick = false
				
				if votesForKick >= math.ceil(#game.Players:GetPlayers()/2) then plrVoted:Kick("You have been votekicked") end
				
				wait(cooldown - timeForVote)
				plrsOnCooldown[plr] = nil
			end
			
		elseif isVotingForKick then
			
			if msg == "yes" then
				print("what")
				if plrsVoted[plr] ~= "yes" then
					
					votesForKick = votesForKick + 1
				end
				plrsVoted[plr] = "yes"
				
			elseif msg == "no" then
				print("what")
				
				if plrsVoted[plr] == "yes" then
					
					votesForKick = votesForKick - 1
				end
				plrsVoted[plr] = "no"
			end		
		end	
	end)
end)```

Where do you put this script? ON module script, script, or local script

It’s inside of a server script.

Did you add a remote event? If no try add it

I’ve tried to add a remote event yet no luck. MP3Face’s solution to it might work but I can’t figure out how to add it to my current script

Change this to this :

script.Parent.yes.Activated:connect(function()
	print("yes")
	votesForKick += 1
	plrsVoted[plr] = "yes"
end)

script.Parent.no.Activated:connect(function()
	print("no")
	votesForKick -= 1
	plrsVoted[plr] = "no"
end)
local votekickCmd = "!votekick"

local cooldown = 60
local plrsOnCooldown = {}

local timeForVote = 10

local isVotingForKick = false
local plrsVoted = {}

local votesForKick = 0

local systemMsgRE = game.ReplicatedStorage["Other Remotes"]:WaitForChild("Votekick")


game.Players.PlayerAdded:Connect(function(plr)
	
	plr.Chatted:Connect(function(msg)
		
		if string.sub(msg, 1, string.len(votekickCmd)) == votekickCmd and not isVotingForKick then
			
			local plrVoted = game.Players:FindFirstChild(string.sub(msg, string.len(votekickCmd) + 2))
			
			if plrVoted and not plrsOnCooldown[plr] then
				
				plrsOnCooldown[plr] = true
				
				isVotingForKick = true
				votesForKick = 0
				
				systemMsgRE:FireAllClients(plrVoted, plr)
				
				wait(timeForVote)
				
				isVotingForKick = false
				
				if votesForKick >= math.ceil(#game.Players:GetPlayers()/2) then plrVoted:Kick("You have been votekicked") end
				
				wait(cooldown - timeForVote)
				plrsOnCooldown[plr] = nil
			end
			
		elseif isVotingForKick then
			
			plr.PlayerGui.Votekick.Yes.Activated:connect(function()
				print("yes")
				votesForKick += 1
				plrsVoted[plr] = "yes"
			end)

			plr.PlayerGui.Votekick.No.Activated:connect(function()
				print("no")
				votesForKick -= 1
				plrsVoted[plr] = "no"
			
			end)		
		end
	end)
end)```
^ My current script

nothing printing so far

Looks like you’d need to separate the statement under “elseif isVotingForKick” into its own function, bound to a remote event. Try removing that portion and adding this to the server script, somewhere outside of the PlayerAdded function:

local voteEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
voteEvent.Name = "voteEvent"

voteEvent.OnServerEvent:connect(function(plr, vote)
	if isVotingForKick then
		if vote=="yes" then
			print(plr.Name .. " voted yes!")
			votesForKick += 1
			plrsVoted[plr] = "yes"
		elseif vote=="no" then
			print(plr.Name .. " voted no!")
			votesForKick -= 1
			plrsVoted[plr] = "no"
		else--Incase vote is nil or anything other than yes/no, catch the error, just for debug purposes:
			print(plr.Name .. " voted... neither???")
		end
	else
		print("not currently voting...")
	end
end)

Then, change my localscript code from above to the following:

local voteEvent = game:GetService("ReplicatedStorage"):WaitForChild("voteEvent")

script.Parent.yes.Activated:connect(function()
	print("I vote yes")
	voteEvent:FireServer("yes")
end)

script.Parent.no.Activated:connect(function()
	print("I vote no")
	voteEvent:FireServer("no")
end)
local votekickCmd = "!votekick"

local cooldown = 60
local plrsOnCooldown = {}

local timeForVote = 5

local isVotingForKick = false
local plrsVoted = {}

local votesForKick = 0

local systemMsgRE = game.ReplicatedStorage["Other Remotes"]:WaitForChild("Votekick")


game.Players.PlayerAdded:Connect(function(plr)
	
	plr.Chatted:Connect(function(msg)
		
		if string.sub(msg, 1, string.len(votekickCmd)) == votekickCmd and not isVotingForKick then
			
			local plrVoted = game.Players:FindFirstChild(string.sub(msg, string.len(votekickCmd) + 2))
			
			if plrVoted and not plrsOnCooldown[plr] then
				
				plrsOnCooldown[plr] = true
				
				isVotingForKick = true
				votesForKick = 0
				
				systemMsgRE:FireAllClients(plrVoted, plr)
				
				wait(timeForVote)
				
				isVotingForKick = false
				
				if votesForKick >= math.ceil(#game.Players:GetPlayers()/2) then plrVoted:Kick("You have been votekicked") end
				
				wait(cooldown - timeForVote)
				plrsOnCooldown[plr] = nil
			end
			
		elseif isVotingForKick then
			
			local voteEvent = game.ReplicatedStorage["Other Remotes"]:WaitForChild("Vote")

			voteEvent.OnServerEvent:connect(function(plr, vote)
				if isVotingForKick then
					if vote=="yes" then
						print(plr.Name .. " voted yes!")
						votesForKick += 1
						plrsVoted[plr] = "yes"
					elseif vote=="no" then
						print(plr.Name .. " voted no!")
						votesForKick -= 1
						plrsVoted[plr] = "no"
					else--Incase vote is nil or anything other than yes/no, catch the error, just for debug purposes:
						print(plr.Name .. " voted... neither???")
					end
				else
					print("not currently voting...")
				end
			end)		
		end
	end)
end)

Only the print(“I vote yes”) printed.

local voteEvent = game:GetService("ReplicatedStorage")["Other Remotes"]:WaitForChild("Vote")

script.Parent.Activated:connect(function()
	print("I vote yes")
	voteEvent:FireServer("yes")
end)

Move my snippet of code outside of the game.Players.PlayerAdded… function, somewhere say, right below the line “local systemMsgRE = …”. Since right now, isVotingForKick = false, so that portion of the code won’t execute, and the event won’t be made/bound.
I realize now this may mean a total rewrite of how this is server script is structured. If it’s still trouble, I can help rewrite some of it.

1 Like

It worked!! Thank you so very much for your help for I couldn’t have done it without you

1 Like