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
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.
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.
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.
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)
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)
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