Getting the player from inside a script

Hi, I am trying to add a feature where if a player owns a gamepass the roll speed is cut in half. The problem i am having is that I try to use game.Players.PlayerAdded:Connect(function(player) to detect the player but for some reason it doesnt work. The script is a server script inside of a gui button and I want it to first detect the player, if they have the gamepass and then act accordingly wether they have it or not. I have tried to just simply put

game.Players.PlayerAdded:Connect(function(player)
	print("Joined")
end)

as a way of testing it but that doesnt work inside the script either. This exact code works in a script elsewhere though. Therefore i assume that due to the location of the script that way of detecting the player wont work but i dont know how else i can do this.

This is my script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FormatNumber = require(ReplicatedStorage.FormatNumber.Simple)
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 1143140303

local canClick = true
script.Parent.MouseButton1Click:Connect(function()
	if not canClick then return end
	canClick = false

	local fakeRewards, reward = game.ReplicatedStorage.Function:Invoke()
	local myString = reward[4]
	local splitted = string.split(myString, ", ")
	local colour = Color3.fromRGB(tonumber(splitted[1]), tonumber(splitted[2]), tonumber(splitted[3]))
	
	script.Parent.Parent.RollFrame.RewardContainer.RewardText.Text = reward[1]
	script.Parent.Parent.RollFrame.RewardContainer.RewardChance.Text = '1 in '..tostring(FormatNumber.FormatCompact(reward[2]))
	script.Parent.Parent.RollFrame.RewardContainer.RewardPrice.Text = '$'..FormatNumber.FormatCompact(reward[3])
	script.Parent.Parent.RollFrame.RewardContainer.RewardText.TextColor3 = Color3.fromRGB(tonumber(splitted[1]), tonumber(splitted[2]), tonumber(splitted[3]))
	game.Workspace.GameTime.Value = 2
	script.Parent.Parent.Parent.Countdown.TextLabel.Visible = true
	script.Parent.Parent.RollFrame.Visible = true
	wait(2)
	script.Parent.Parent.RollFrame.Visible = false
	script.Parent.Parent.Parent.Countdown.TextLabel.Visible = false
	canClick = true
end)

The reason why Players.PlayerAdded is failing in this scenario is that the player you intend to work with is already in the server. Your script doesn’t begin execution until the GUI has been cloned from StarterGui into the player’s PlayerGui. PlayerGui objects can only exist inside a Player instance, which represents a connected client.

This method of deriving a Player instance is inappropriate for your use-case as it also targets all future clients, severing the link between the GUI and its associated user.


The server is not responsible for programming the user interface.

The user interface (UI) is rendered and operated on the client’s device. This means all UI-related property changes must happen client-side. Involving the server introduces latency into a client-facing system, degrading the user experience. Additionally, server involvement will hit replication boundaries, making certain UI features impossible to implement.

You should always use a LocalScript to handle UI logic.


TL;DR

Refactor your setup to use a RemoteFunction and convert your script into a LocalScript. From there:

  • Use Players.LocalPlayer to access the active client’s Player instance
  • Or, for server-side code, use the Player argument passed via RemoteEvent.OnServerEvent