so I have a script that when you click a button it gives you 250 coins when you down own the pass, but 500 if you do. however, the script is failing to check if the player owns the gamepass. I have tried many different ways of doing this, and ive looked around a bit, but nothing has worked.
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local gamePassId = 678241646
local success, productInfo = pcall(function()
return game:GetService("MarketplaceService"):GetProductInfo(gamePassId)
end)
if success and productInfo then
local hasGamepass = player:HasPass(gamePassId)
local coinsToAdd
if hasGamepass then
coinsToAdd = 500
else
coinsToAdd = 250
end
local coins = player.leaderstats.Coins
if coins then
coins.Value = coins.Value + coinsToAdd
print("Added", coinsToAdd, "coins to", player.Name)
else
print("Leaderstat 'Coins' not found for player:", player.Name)
end
else
print("Failed to retrieve gamepass information for ID:", gamePassId)
end
end)
I see. I recommend using UserOwnsGamepassAsync() again, though making sure you provide the necessary parameters.
UserOwnsGamepassAsync requires the following arguments:
1 - The User ID of the player you want to check.
2 - The Gamepass ID you want to see if the player owns.
Used correctly, it should look like this: game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.UserId, gamePassId)
I also have another question. Is this script a LocalScript or is it on the server? If itâs a regular Server Script, then attempting to call :HasPass() would return the error as game.Players.LocalPlayer would be nil.
itâs a regular script. I just used UserOwnsGamepassAsync(player.UserId, gamePassId), and it gave an error saying âattempt to index nil with âUserIdââ
Aha! So that is indeed the problem. LocalPlayer is a property that only exists on the client, so a regular Server Script wonât have any value for it. Weâll need to first get a variable for the player that interacts with your button.
On line 1, where the MouseButton1Click event is invoked, add a variable inside of function(). It should then look like this:
Then you can remove the statement for local player = game.Players.LocalPlayer. Now the error should be fixed!
Iâd also recommend moving the pcall for GetProductInfo to the beginning of the script, outside of the event code, as it would unnecessarily run it every time the button is clicked.
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local gamePassId = 678241646
if success and productInfo then
local hasGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamePassId)
local coinsToAdd
if hasGamepass then
coinsToAdd = 500
else
coinsToAdd = 250
end
local coins = player.leaderstats.Coins
if coins then
coins.Value = coins.Value + coinsToAdd
print("Added", coinsToAdd, "coins to", player.Name)
else
print("Leaderstat 'Coins' not found for player:", player.Name)
end
else
print("Failed to retrieve gamepass information for ID:", gamePassId)
end
end)
Ps: Iâve added comments explaining what things do.
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer -- Gets the local player.
local gamePassId = 678241646 -- This is the game pass.
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamePassId) then -- this will check if they have it. If they do, it will do the statement. If not, it will do the else statement.
player.leaderstats.Cash.Value += 500
else -- This is the else statement that will happen if the user does not own the game pass.
plr.leaderstats.Cash.Value += 250
end
end)
I do highly recommend firing a remote event to change the cash value, as using a local script to do so will only change it for the Client, and isnât actually there.
the changes I made should indeed work.
If you experience issues, please let me know.
I also shortened it / simplified it, because the line where you were getting the product info was never used.
That line mightâve caused additional problems in the past or couldâve in the future as it wasnât being used.
Personally, I find this uneccesary, and could cause confusion and unoptomization in code causing the game to lag as itâs running unecessary if else statements.
To sum it up, instead of doing the if hasGamepass then coinsToAdd = 500 etc function, you could instead do what I put in the new script which is just immeadiately changing it.
You also need to insert a LocalScript into the UI as it will not work otherwise.
Okay, I can better understand the problem now. The main error occurring here is that the event is being invoked on a Script and not a LocalScript. Because of this, we arenât able to get the Instance of the player clicking your button.
What we should do is create a LocalScript with the event to run when the button is clicked, which we can then use game.Players.LocalPlayer for. Then we should fire a RemoteEvent to communicate to the server that someone clicked the button.
Then, as @King_GamingRobloxYT suggested, add a Script on the server with a function to run when the RemoteEvent is fired, and then do the calculation as to how to add the values.
ive put it into a server script, with a localscript activating a remote event in the button itself. it still errors with the marketplaceservice though, with the error of âattempt to index nil with âUserIdââ
LocalPlayer doesnât exist on the server. If youâre sending a remote event, the first argument passed should automatically be the player there and you can check it that way.