So for my obby game, I have two gamepasses so far. One for a speed coil and one for a gravity coil. The only problem is, for some reason, is that when you buy one gamepass, it gives you the other coil when you don’t even own it. I singled out the problem to be the server side script but I am unsure how to fix it. Here is the script down below for reference:
local mps = game:GetService("MarketplaceService")
local gamepassId = script.GamepassID.Value
mps.PromptGamePassPurchaseFinished:Connect(function(plr,passId,purchased)
if purchased then
script.GravityCoil:Clone().Parent = plr.Backpack
end
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local ownsPass = mps:UserOwnsGamePassAsync(plr.UserId,gamepassId)
if ownsPass then
script.GravityCoil:Clone().Parent = plr.Backpack
end
end)
end)
That is the server script which also has a string and the tool itself inside the script. It is the same script for the speed coil but separate entirely. If there is anything else needed, just me know and I will be sure to provide it.
yep! I deleted the string too so the script itself contains the numbers to the gamepass to see if that would help but it didn’t. I’m still not sure what the problem is
Can you provide a video of the issue? I want to see what happens when you purchase the gamepass. Also, rather than using if ownsPass, I reccomend using a datastore, as it will erorr less
I’m not sure how to record a video of what is happening but pretty much when I press the buy button in my shop, it pops up the prompt asking if I want to buy the gamepass:
As you can see by the screenshot above, it gave me both coils. I’m still a beginner scripter so I’m not sure how I would script the datastore instead of the ownsPass. Do you believe that is the reason why it is giving me both passes?
I don’t think the datastore is the reason why, as that would just make it easier. To script the datastore, find a datastore tutorial for saving BoolValues, then check if these boolvalues are ticked on join, and if they are, give the player the items.
As for the erorr, all the code works fine. It is most likely due to the id being the same for the speed coil and the gravity coil gamepass. Can you send the code for the speed coil as well? If it isn’t that, it must be something to do with one of the UI elements.
Here is the id for the gravity coil : 161520885
Here is the id for the speed coil : 161521076
The script for the gravity coil ui button is this:
local mps = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local gamepassId = 161520885
script.Parent.MouseButton1Click:Connect(function()
local owns = mps:UserOwnsGamePassAsync(plr.UserId,gamepassId)
mps:PromptGamePassPurchase(plr,gamepassId)
if owns then
script.Parent.Text = "Gravity Coil Owned"
end
end)
And vice versa with the speed coil:
local mps = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local gamepassId2 = 161521076
script.Parent.MouseButton1Click:Connect(function()
local owns = mps:UserOwnsGamePassAsync(plr.UserId,gamepassId2)
mps:PromptGamePassPurchase(plr,gamepassId2)
if owns then
script.Parent.Text = "Speed Coil Owned"
end
end)
Edit: So I did more testing and I know for a fact that the server scripts are the ones that are causing the problem. No matter if I click the speed coil purchase or the gravity coil one, both scripts run at the same time. I believe you mentioned that because I am using the same script, give or take a few things, they are both running at the same time since they relatively do the same thing.
Maybe in this case you should just make it only one script and local script that fires a remotefunction with a return from the script, something like this would work:
LOCALSCRIPT
local plr = game.Players.LocalPlayer
local gamepassId1 = 161520885
local gamepassId2 = 161521076
local remoteFunction = PATH.TO.REMOTEFUNCTION
local speedcoilbutton = PATH.TO.BUTTON.OF.SPEEDCOIL
local gravitycoilbutton = PATH.TO.BUTTON.OF.GRAVITYCOIL
speedcoilbutton.MouseButton1Click:Connect(function()
local Owns = remoteFunction:InvokeServer(gamepassId1)
repeat wait() until Owns ~= nil
if Owns == false then return end
PATH.TO.TEXT.Text = "Gravity Coil Owned"
end)
gravitycoilbutton.MouseButton1Click:Connect(function()
local Owns = remoteFunction:InvokeServer(gamepassId2)
repeat wait() until Owns ~= nil
if Owns == false then return end
PATH.TO.TEXT.Text = "Speed Coil Owned"
end)
SERVERSCRIPT
local mps = game:GetService("MarketplaceService")
local remoteFunction = PATH.TO.REMOTEFUNCTION
remoteFunction.OnServerInvoke:Connect(function(id)
if not id return end
local Owns = mps:UserOwnsGamePassAsync(plr.UserId,id)
if Owns then
return Owns
else
mps:PromptGamePassPurchase(plr,id)
return Owns
end
end)
mps.PromptGamePassPurchaseFinished:Connect(function(plr,passId,purchased)
if purchased then -- did you checked for gamepass ID?
script.GravityCoil:Clone().Parent = plr.Backpack
end
end)
some developers forgetting to check the gamepass ID,
use:
if purchased then
if passId == gravity coil ID and not mps:UserOwnsGamePassAsync(plr.UserId, gravity coil ID) then
-- code
elseif passId == speed coil ID and not mps:UserOwnsGamePassAsync(plr.UserId, speed coil ID) then
-- code
end
end
put this instead of
if purchased then
script.GravityCoil:Clone().Parent = plr.Backpack
end
Ah that ended up working! Thank you so much. The way you set it up made a lot of sense too and it makes sense as well why the script wasn’t working in the first place. I’ll mark your answer as a solution! Thanks again for the help!
Hi! So it seems I have encountered a slight problem that I only sort of noticed now. Everything works as it should except for one thing: When you reset your character, it doesn’t give the coil back to you. How would I go about fixing that? I’ll include my updated script that @Reytinaz helped with:
local mps = game:GetService("MarketplaceService")
local gamepassId = 161520885
mps.PromptGamePassPurchaseFinished:Connect(function(plr,gamepassId,wasPurchased)
if wasPurchased then
if gamepassId == 161520885 and not mps:UserOwnsGamePassAsync(plr.UserId, 161520885) then
print("Gravity")
script.GravityCoil:Clone().Parent = plr.Backpack
elseif gamepassId == 161521076 and not mps:UserOwnsGamePassAsync(plr.UserId, 161521076) then
print("Speed")
script.SpeedCoil:Clone().Parent = plr.Backpack
end
end
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local ownsPass = mps:UserOwnsGamePassAsync(plr.UserId,161520885)
if ownsPass then
script.GravityCoil:Clone().Parent = plr.Backpack
end
end)
end)
plr.CharacterAdded:Connect(function()
local ownsPass = mps:UserOwnsGamePassAsync(plr.UserId,161520885)
if ownsPass then
script.GravityCoil:Clone().Parent = plr.Backpack
end
end)
and put this into an event mps.PromptGamePassPurchaseFinished,
just replace
if gamepassId == 161520885 and not mps:UserOwnsGamePassAsync(plr.UserId, 161520885) then
print("Gravity")
script.GravityCoil:Clone().Parent = plr.Backpack
script.GravityCoil:Clone().Parent = plr.StarterGear
elseif gamepassId == 161521076 and not mps:UserOwnsGamePassAsync(plr.UserId, 161521076) then
print("Speed")
script.SpeedCoil:Clone().Parent = plr.Backpack
script.SpeedCoil:Clone().Parent = plr.StarterGear
end
oh yeah, if player will join, he need his gamepasses, so
game.Players.PlayerAdded:Connect(function(plr)
wait(1) -- waits for player loads
local ownsPassGravity = mps:UserOwnsGamePassAsync(plr.UserId,161520885)
if ownsPass then
script.GravityCoil:Clone().Parent = plr.Backpack
script.GravityCoil:Clone().Parent = plr.StarterGear
end
local ownsPassSpeed = mps:UserOwnsGamePassAsync(plr.UserId,161521076 )
if ownsPass then
script.SpeedCoil:Clone().Parent = plr.Backpack
script.SpeedCoil:Clone().Parent = plr.StarterGear
end
end)