Scripting trouble with UserOwnsGamePassAsync

Hey! I’m trying to make a script to if a player does not own a gamepass they’re rewarded +1 coin but if they own a gamepass, they’re rewarded +2

This is my script:

local DataStore2 = require(game.ServerScriptService.DataStore2)


script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		
		
		local pointsStore = DataStore2("MovieBux", player)
		print("Granted 1 Coin")
		
		pointsStore:Increment(1) -- Give them 1 point
		
		script.Parent.Transparency = 1
		script.Disabled = true
		wait(waittime)
		script.Parent.Transparency = 0
		script.Disabled = false	
		
	else
		local GamepassID = 10951455
		local Player = game.Players.LocalPlayer
		local pointsStore = DataStore2("MovieBux", player)
		
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player, GamepassID) then
			
			
			pointsStore:Increment(2) -- Give them 2 point
			print("Granted 2 Coins")
			
			script.Parent.Transparency = 1
			script.Disabled = true
			wait(waittime)
			script.Parent.Transparency = 0
			script.Disabled = false		
			
	end
	
	
			
		
	end
end)

The script will work but only reward the player with one of the coins, I’m unsure why this is? Any help would greatly be appreciated… thank you! <3

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    local GamepassID = 10951455
    local hasGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player, GamepassID)

    if not hasGamepass then
       --your code if the player does not have gamepass
    else
        --your code if the player does have gamepass
    end
end

p.s. idk if the api is correct, I just copied and pasted from your code.

edit: I am not sure what :UserOwnsGamePassAsync returns, so you might need to change the if statement

1 Like

Did you put this code inside of a script or local script? Cause you’ll need to put it into a script, not a local script.

1 Like

It’s just in a script, not a local script.

1 Like

Oh, well then I catched an error right there. You wrote local Player = game.Players.LocalPlayer, but you can’t get the local player from a normal script. Fix that first. Also follow @Ocipa’s answer, that should make the gamepass-check work.

Thanks! Although, I’m now getting this error:

image

I have not modified the script at all just yet.

You have too many ends. Remove the second to last one. (You just need two, one to close the if-else statement and one to close the connected function. Three will throw an error.)

The real issue here is the confusing indentation. Ignore this post, I was wrong.

3 Likes

in the quick code I put up, I forgot to add a ) after the last end to close the the .touched event

1 Like

Hi! I removed the 2nd to last ‘end’ and received this error: image

Then add else into that script Like the error says so

else is included in the script, aha

Where is the else at the end then?

My bad, I was thrown off by the indentation. The problem with the original code is you check if the player exists, and if they don’t you check if they have the gamepass. Checking for a gamepass should be in the if, not the else.

2 Likes

Oh and also you should make an touch event (basically make a script that fires the remote event everytime the part is touched and then make a local script that does something when the remote event is fired) and move those lines of script in the local script one (except the script.Parent.Touched line and the end)

No, a LocalScript should not be the one handling DataStores.

2 Likes

Ah, all still trial and error… does this seem slightly more right than the original?


local waittime = 60

local GamepassID = 10951455
local Player = game.Players.LocalPlayer


script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		
		
		local pointsStore = DataStore2("MovieBux", player)
		print("Granted 1 Coin")
		
		pointsStore:Increment(1) -- Give them 1 point
		
		script.Parent.Transparency = 1
		script.Disabled = true
		wait(waittime)
		script.Parent.Transparency = 0
		script.Disabled = false	
		
		if
			
		
	
		
		 game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player, GamepassID) then
			
			
			pointsStore:Increment(2) -- Give them 2 point
			print("Granted 2 Coins")
			
			script.Parent.Transparency = 1
			script.Disabled = true
			wait(waittime)
			script.Parent.Transparency = 0
			script.Disabled = false		
		else
			
		end
		
		
		
		
	end
end)
1 Like

Also if you disable the script then the script won’t be able to these following lines unless another script enables it Like the script will still be able to read the lines but it won’t make the part visible/invisible because it’s obviously disabled

You can test it and see if it errors, but that should work fine. You might want to move the gamepass code above the 60 second wait, though.

2 Likes

Thanks! It’s still printing that it’s gave 1 coin with no errors?