Value adds 2 if there's 2 players, it's only supposed to add one

local GroupId = 5162592
local AllowedRank = 103
game.Players.PlayerAdded:Connect(function(Player)
	script.Parent.MouseButton1Click:Connect(function()
		if Player:GetRankInGroup(GroupId) >= AllowedRank then	
			if script.Parent.Parent.SlideNum.Value >= 1 then
				script.Parent.Parent.SlideNum.Value = script.Parent.Parent.SlideNum.Value - 1
				print(script.Parent.Parent.SlideNum.Value)
			end
		end
	end)
end)

The concept:
We have a slideshow GUI that changes when you press NEXT if you’re a certain rank.

The issue:
If 2 eligible ranks are in game, it will skip 2 every time NEXT is clicked.

What I think is happening:
PlayerAdded is somehow messing with this system.

You’re creating a new connection every time a player is added. You don’t need a .PlayerAdded connection.

How would I identify if a user is the correct rank then?

When the button is clicked, check if the LocalPlayer is of rank, if the player is of rank, then fire some remote and add 1 to the value. Also, before this, check again on the server that the player who fired the remote has the rank just as a security measure.

You shouldn’t be accessing the PlayerGui on the server either.

It’s a surface GUI, so I’d thought I do it via serversided. Thanks!

Interactive SurfaceGuis shouldn’t be handled on the server either. Good practice is to create a SurfaceGui, parent it to the player’s PlayerGui and adorn it to the part, and handle it via LocalScripts. If you have to communicate with the server and/or other clients, you want to use remote events or functions (with the exception of :InvokeClient which is also generally bad practice to use, use RemoteEvent:FireClient/FireAllClients instead).

I am still pretty new to scripting, so I have no idea what half of that means. I am genuinely sorry. I’m more of a builder.

1 Like

Basically, for interactive SurfaceGuis, put them in StarterGui and set the SurfaceGui’s adornee property to a part, and handle the SurfaceGui’s events with a LocalScript.

I have no idea how I would do that. Is there any way I could do it the way I was doing, only via server script? I tried doing local, but the events didn’t fire even though there was no errors and everything was correct as according to the developer hub.

You can but it’s bad practice, there’s also no way to know what player clicked the button.

LocalScripts don’t work in the workspace either.
" A LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player’s Backpack , such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts .
  • The ReplicatedFirst service"

https://developer.roblox.com/en-us/api-reference/class/LocalScript

1 Like

Is there a guide to the “good practice”? Because I am either tired, or I still can’t understand half of what you’re saying.

Nevermind, I understand now. I found the interactive surface GUI on the devhub.

1 Like

For others with a similar issue.