How do I change every player's UI when a player clicks a button?

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

  1. What do you want to achieve? Keep it simple and clear!
    Upon clicking on a button, the button’s text will change and every player’s button text will change as well.
  2. What is the issue? Include screenshots / videos if possible!
    Wouldn’t change for every player, only the client.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have, and tried to see if I could use RemoteEvents it wouldn’t work though

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

LocalScript:

local button = script.Parent.Parent.PauseButton
local pausebool = game.ReplicatedStorage:WaitForChild("Paused")
local Event = game.ReplicatedStorage:WaitForChild("PauseEvent")

-- functions
function pause(a,b,c,d)
	for _,player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character
		character.Humanoid.WalkSpeed = a
		character.Humanoid.JumpHeight = b
		pausebool = c
		button.Text = d
	end
end

-- button scripts
button.MouseButton1Click:Connect(function()
	if pausebool == false then
		pause(0,0,true,"Unpause")
	else
		pause(16,7.2,false,"Pause")
	end
end)

ServerScript:

local PauseEvent = game.ReplicatedStorage:WaitForChild("PauseEvent")
local PlayersService = game:GetService("Players")

local player = PlayersService.LocalPlayer
local pausebool = game.ReplicatedStorage:WaitForChild("Paused")

PauseEvent:FireAllClients(function()
	if pausebool == false then
		for _,player in ipairs(game.Players:GetPlayers()) do
			local character = player.Character
			character.Humanoid.WalkSpeed = 0
			character.Humanoid.JumpHeight = 0
			pausebool = true
			player.PlayerAdded:Connect(function(Player)
				Player:WaitForChild('PlayerGui'):WaitForChild('MainUI').PauseButton.Text = "Unpaused"
			end)
		end
	else
		for _,player in ipairs(game.Players:GetPlayers()) do
			local character = player.Character
			character.Humanoid.WalkSpeed = 16
			character.Humanoid.JumpHeight = 7.2
			pausebool = false
			player.PlayerAdded:Connect(function(Player)
				Player:WaitForChild('PlayerGui'):WaitForChild('MainUI').PauseButton.Text = "Pause"
			end)
		end
	end
end)

I also don’t know a lot about scripting yet, I am still learning.

1 Like

Insert a RemoteEvent inside ReplicatedStorage called “PauseEvent”

2 Likes

There’s already a RemoteEvent called PauseEvent inside of the ReplicatedStorage.

2 Likes

Try this out:
server

local PauseEvent = game.ReplicatedStorage:WaitForChild("PauseEvent")
local pauseBool = game.ReplicatedStorage:WaitForChild("Paused")


--[[
	Updates whether or not the given player's character is paused or not!
]]
local function updateCharacterState(player)
	-- This makes sure our thread doesn't yield
	task.spawn(function()
		-- If their character object doesn't exist, we wait until it does
		if (player.Character == nil) then
			player.CharacterAdded:Wait()
		end

		player.Character:WaitForChild("HumanoidRootPart").Anchored = pausebool.Value
	end)
end

-- Listens for when a player presses the pause / unpause button!
PauseEvent.OnServerEvent:Connect(function(sender: Player, isPaused: boolean)
	pauseBool.Value = isPaused

	for _, player in ipairs(game.Players:GetPlayers()) do
		updateCharacterState(player)

		PauseEvent:FireClient(player, isPaused)
	end
end)

-- This makes sure new players get paused as well
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		updateCharacterState(player)
	end)
end)

client

local button = script.Parent.Parent.PauseButton
local pauseBool = game.ReplicatedStorage:WaitForChild("Paused")
local Event = game.ReplicatedStorage:WaitForChild("PauseEvent")


local function updatePausedState(isPaused: boolean)
	button.Text = (isPaused and "Unpause") or "Pause"
	pauseBool.Value = isPaused
end

-- button scripts
button.MouseButton1Click:Connect(function()
	-- Toggles between unpaused / paused
	updatePausedState(not pauseBool.Value)

	-- Telling the server we just updated the paused state!
	Event:FireServer(pauseBool.Value)
end)

Event.OnClientEvent:Connect(function(isPaused)
	updatePausedState(isPaused)
end)
2 Likes

It works now, thank you! I also had to fix the captialization one of the lines, but overall it worked as intended. Thanks!

1 Like