Need help with a scripting problem

Hey there!
I’m Shark, and I need help.
So, I’m not the best at scripting, but I try my best.
I need help with this script, basically, what I need it to do is : When you type the word “!startrules” in the chat, a GUI will become visible and it will display what I have on it and when you say the word “!stoprules” the GUI will become invisible. But for some reason, it doesn’t seem to work.
Sorry if I’ve done something wrong, but here’s my script.

local GroupId = 8266373
local MinimumRank = 252


game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(GroupId) >= MinimumRank then
			if Message == "!startrules" then
				game.StarterGui.Rules.Rule1.Visible = true
			elseif Message == "!stoprules" then
				game.StarterGui.Rules.Enabled = false
			end
		end
	end)
end)

I would really appreciate some help, if someone could help me fix it, it would be appreciated and once again, apologies if I have done something wrong.
Thank you.

1 Like

First of all, is this in a local script?

Because GUIs only work in local script

Oh!! Okay, let me try that and see if it works, it was in a normal script.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message, plr)
		if plr:GetRankInGroup(GroupId) >= MinimumRank then
			if Message == "!startrules" then
				game.StarterGui.Rules.Rule1.Visible = true
			elseif Message == "!stoprules" then
				game.StarterGui.Rules.Enabled = false
			end
		end
	end)
end)

Don’t use StarterGui, use PlayerGui.

for _, plr in pairs(game.Players:GetPlayers()) do
  local playerGui = plr.PlayerGui
  -- do stuff
end

And another thing, when you add it in local script, you should do game:GetService(“Players”).LocalPlayer.PlayerGui instead of game:GetService(“StarterGui”) because the PlayerGui the player has is the current GUI that is shown to them

You’re using StarterGui rather than the GUI located in the Player, StaterGui should only be changed if you want to change something for everyone after a respawn since it gets replicated, but in this case, you only have to change it for a single player, so PlayerGui is what you need to use. Keep the code in a regular script but change it to this

local GroupId = 8266373
local MinimumRank = 252


game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(GroupId) >= MinimumRank then
			if Message == "!startrules" then
				Player.PlayerGui.Rules.Rule1.Visible = true
			elseif Message == "!stoprules" then
				Player.PlayerGui.Rules.Enabled = false
			end
		end
	end)
end)

But he wants everyone to see the rules.

everything in StarterGui gets replicated to PlayerGui, whatever you change there, no one would see

If you want everyone to see the rules DONT convert this script into a local script, just use RemoteEvent:FireAllClients()

Oh

This wouldn’t work, even if this was in a local script the game would have FE enabled so the things 1 local script does wouldn’t replicate to the other players

The script I provided was meant to be used in a server script

This code will go in a Script

If you want a custom GUI to show, you will have to fire a RemoteEvent, then in a Local Script pick up the RemoteEvent and display the GUI.

This wouldn’t work either, GUIs should ONLY be worked with in local scripts

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message, plr)
		if plr:GetRankInGroup(GroupId) >= MinimumRank then
			if Message == "!startrules" then
				for i, v in pairs(game.PLayers:GetChildren()) do
                    v:FindFirstChild("PlayerGui").StarterGui.Rules.Visible = true
                end
			elseif Message == "!stoprules" then
				for i, v in pairs(game.PLayers:GetChildren()) do
                    v:FindFirstChild("PlayerGui").StarterGui.Rules.Visible = false
                end
			end
		end
	end)
end)

So is what I’m trying to do not possible?

1 Like

Also if that works, give the solution to @HugeCoolboy2007

It appears you don’t fully understand how Client-Server works

this should help