Gamepass script

Hi I’m new to scripting. I have tried a lot of things but still I can’t. If someone has a certain game pass it gives more energy to certain tools. But if someone has the game pass, the whole server gets more energy. And I want only the player who has the game pass to get more energy.
This is my script.

local mps = game:GetService("MarketplaceService")
local Items = game.ServerStorage.Items

game.Players.PlayerAdded:connect(function(Player)
if mps:UserOwnsGamePassAsync(Player.UserId,9002245)then
	Items.Starterfedora.Configure.Energy.Value = 10
	Items.Darkfedora.Configure.Energy.Value = 40
	Items.Mafiafedora.Configure.Energy.Value = 120
	Items.Basicfedora.Configure.Energy.Value = 200
	Items.Purplefedora.Configure.Energy.Value = 300
	Items.Bluefedora.Configure.Energy.Value = 470
	Items.Redfedora.Configure.Energy.Value = 600
	Items.Whitefedora.Configure.Energy.Value = 1000
	Items.Darkbluefedora.Configure.Energy.Value = 2000
	Items.Goldenfedora.Configure.Energy.Value = 5000
	game.ServerStorage.Leo.Configure.Energy.Value = 500
	game.ServerStorage.Rainbow.Configure.Energy.Value = 50000
if mps:UserOwnsGamePassAsync(Player.UserId,9002265)then
	Items.Starterfedora.Configure.Energy.Value = 25
	Items.Darkfedora.Configure.Energy.Value = 100
	Items.Mafiafedora.Configure.Energy.Value = 300
	Items.Basicfedora.Configure.Energy.Value = 500
	Items.Purplefedora.Configure.Energy.Value = 750
	Items.Bluefedora.Configure.Energy.Value = 1175
	Items.Redfedora.Configure.Energy.Value = 1500
	Items.Whitefedora.Configure.Energy.Value = 2500
	Items.Darkbluefedora.Configure.Energy.Value = 5000
	Items.Goldenfedora.Configure.Energy.Value = 12500
	game.ServerStorage.Leo.Configure.Energy.Value = 1250
	game.ServerStorage.Rainbow.Configure.Energy.Value = 125000
end
end
end)

You should create a remote event, then fire the client if the player owns the game pass. On a local script, change these values and then it will only apply to that player.

How do I do that? Can you show me an example?

Player.Backpack.tool should work.

Make a RemoteEvent in ReplicatedStorage. Call it purchaseHandler

In your purchase handler, change the config to:

game.ReplicatedStorage.purchaseHandler:FireClient(Player)

Next, make a local script on the client. In this script put:

game.ReplicatedStorage.purchaseHandler.OnClientEvent:Connect(function()
      -- put all of your configure changes in here
end)

Sorry if this isn’t the best help! You might have to make two different remotes or just pass an argument if you have more than one gamepass changing local configuration.

If the player does not have certain tools, an error will occur. so that’s not going to work.

Then use

if player.Backpack:FindFirstChild("TheTool") then
   --Your Code Here.

this is my new script:
error:
[ ServerScriptService.Script:9: attempt to perform arithmetic (add) on userdata and number]

game.Players.PlayerAdded:Connect(function(Player)
	local Energy = Player:WaitForChild("leaderstats").Energy
	local Id = 9002245
	local multiple = 0
	local n = 0
	local mps = game:GetService("MarketplaceService")
	if mps:UserOwnsGamePassAsync(Player.UserId,Id) then
		multiple = 2
		Energy = Energy + n * multiple
	end
end)

in this line

if mps:UserOwnsGamePassAsync(Player.UserId,9002245)

Well for checking if UserOwnsGamePassAsync you don’t need to add the player’s UserId as a parameter instead add the main player as the first parameter. So it should look like this

if mps:UserOwnsGamePassAsync(Player,9002245)

also use elseif instead of having multiple if statement at a single scope so you won’t have to put tons of ends. Your script should look like this

local mps = game:GetService("MarketplaceService")
local Items = game.ServerStorage.Items

game.Players.PlayerAdded:Connect(function(Player)
    if mps:UserOwnsGamePassAsync(Player,9002245) then
	Items.Starterfedora.Configure.Energy.Value = 10
	Items.Darkfedora.Configure.Energy.Value = 40
	Items.Mafiafedora.Configure.Energy.Value = 120
	Items.Basicfedora.Configure.Energy.Value = 200
	Items.Purplefedora.Configure.Energy.Value = 300
	Items.Bluefedora.Configure.Energy.Value = 470
	Items.Redfedora.Configure.Energy.Value = 600
	Items.Whitefedora.Configure.Energy.Value = 1000
	Items.Darkbluefedora.Configure.Energy.Value = 2000
	Items.Goldenfedora.Configure.Energy.Value = 5000
	game.ServerStorage.Leo.Configure.Energy.Value = 500
	game.ServerStorage.Rainbow.Configure.Energy.Value = 50000
  elseif mps:UserOwnsGamePassAsync(Player,9002265) then
    Items.Starterfedora.Configure.Energy.Value = 25
    Items.Darkfedora.Configure.Energy.Value = 100
	Items.Mafiafedora.Configure.Energy.Value = 300
	Items.Basicfedora.Configure.Energy.Value = 500
	Items.Purplefedora.Configure.Energy.Value = 750
	Items.Bluefedora.Configure.Energy.Value = 1175
	Items.Redfedora.Configure.Energy.Value = 1500
	Items.Whitefedora.Configure.Energy.Value = 2500
	Items.Darkbluefedora.Configure.Energy.Value = 5000
	Items.Goldenfedora.Configure.Energy.Value = 12500
	game.ServerStorage.Leo.Configure.Energy.Value = 1250
	game.ServerStorage.Rainbow.Configure.Energy.Value = 125000
   end
end)

The script works perfectly. I just don’t want the whole server to get the extra Energy. Only the player who has the game pass.

What? Only the player who has the pass? Bruh why did you put all the items in serverstorage if you want the change to only occur for the player who has the pass? Also if you didn’t know the whole server will receive the changes but not the players (Including the player who has the pass) because clients cannot access ServerStorage therefore you need to use RemoteEvents or RemoteFunctions to access ServerStorage by making a ServerScript pickup a request from the client.

I put all stuff in server storage, because I have a tool shop.

But who has a fix for this.
error:
[ ServerScriptService.Script:9: attempt to perform arithmetic (add) on userdata and number]

game.Players.PlayerAdded:Connect(function(Player)
	local Energy = Player:WaitForChild("leaderstats").Energy
	local Id = 9002245
	local multiple = 0
	local n = 0
	local mps = game:GetService("MarketplaceService")
	if mps:UserOwnsGamePassAsync(Player.UserId,Id) then
		multiple = 2
		Energy = Energy + n * multiple
	end
end)

You don’t reference Energy’s Value property, hence the error.

Energy.Value = Energy.Value + n * multiple