Increase number GUI for all players?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to make it so when a player clicks a GUI a number increases on that GUI for all players in the game.

  2. What is the issue?
    My issue is I have no idea how I would go about doing this I just need a tip of the sorts to know what I should do.

  3. What solutions have you tried so far?
    I’ve checked all over the Developer Hub but I have found nothing that really answers my question.
    I’ve tried remote events but I may not be doing it right.

Fire a remoteevent from the server to all clients using :FireAllClients(), with an argument of the players number. On the client, when it receives it, set the players playergui number guis text to the argument (number)

1 Like

kind of new to scripting but do you think this would work?

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local voteEvent = ReplicatedStorage.DesertVote

local votes = 0

function vote()
	votes += 1
	voteEvent:FireAllClients(votes)
end

script.Parent.MouseButton1Click:Connect(vote)

LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local voteEvent = ReplicatedStorage.DesertVote

local counter = script.Parent

voteEvent.OnClientEvent:Connect(function(plr, votes)
	counter.Text = votes
end)

Think that should work.
You can also put a IntValue in ReplicatedStorage named Votes

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local votesV = ReplicatedStorage.DesertVotes

script.Parent.MouseButton1Click:Connect(function()
     votesV.Value += 1
end)

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local votesV = ReplicatedStorage.DesertVotes

local counter = script.Parent

votesV.Changed:Connect(function()
	counter.Text = votesV.Value
end)
1 Like

wow why didn’t I think of this sooner? lol; thank you!

Edit: is there anyway I could stop the player from voting again until allowed?

where is the server script located?

inside of the button, the local script is inside of the counter.

well to disable the ability to vote for a specific player until allowed.
we first need get the player that pressed the button somehow

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local votesV = ReplicatedStorage.DesertVotes

local disabledVoters = {}

local function setPlayerAllowedToVote(player: Player, allowed: boolean)
    disabledVoters[player] = not allowed or nil
end

local function playerAllowedToVote(player: Player): boolean
    return not disabledVoters[player]
end

local function resetDisabledVoters()
    disabledVoters = {}
end

script.Parent.MouseButton1Click:Connect(function()
    local player = script:FindFirstAncestorWhichIsA("Player")
    if playerAllowedToVote(player) then
          votesV.Value += 1
          setPlayerAllowedToVote(player, false)
    end
end)

Spreading the statements out in functions is unnecessary but it gives more context

1 Like

ah okay that makes sense; thank you so much!

1 Like

This is going to require more client-to-server communication, client scripts are unique to each client so this implementation won’t work.

--ServerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickRemote = ReplicatedStorage:WaitForChild("ClickRemote")
local Clicks = ReplicatedStorage:WaitForChild("Clicks")

ClickRemote.OnServerEvent:Connect(function()
	Clicks.Value += 1
end)
--LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickRemote = ReplicatedStorage:WaitForChild("ClickRemote")
local Clicks = ReplicatedStorage:WaitForChild("Clicks")
local ClicksGuiTextLabel = ... -- Define your textlabel

script.Parent.MouseButton1Click:Connect(function()
	ClickRemote:FireServer()
end)

Clicks:GetPropertyChangedSignal("Value"):Connect(function()
	ClicksGuiTextLabel.Text = Clicks.Value
end)
ClicksGuiTextLabel.Text = Clicks.Value

ServerScripts can be parented to a GuiObject an will be run by server so the values changes will be replicated I see no problem here. And normally I also would use RemoteEvents, but not long ago I found that it is cleaner to do it via BaseValue Instances. But thats my preference you can achieve the same thing with remote events.

And the code can be simplified now that I look back.

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local votesV = ReplicatedStorage.DesertVotes

local voted = false

script.Parent.MouseButton1Click:Connect(function()
	if not voted then
		votesV.Value += 1
		voted = true
	end
end)


--- Example reset counter
while task.wait(10) do
	voted = false
	votesV.Value = 0
end

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local votesV = ReplicatedStorage.DesertVotes

local counter = script.Parent

votesV.Changed:Connect(function()
    counter.Text = votesV.Value
end)

Example.rbxl (42.5 KB)

I thought this was a local script due to it being related to the client’s UI (UI should be handled by the client) and the reference to the ‘ReplicatedStorage’ container, for game logic the ‘ServerStorage’ container should be used instead.

1 Like