You can write your topic however you want, but you need to answer these questions:
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.
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.
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)
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)
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
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)
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.