Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can somebody take a second look at my Gamepass code and see what the issue is?

Asked by 4 years ago

I've just finished creating this code that should find a folder named "Tools" in the ReplicatedStorage and give them the gear within the folder but it isn't working?

Code:

local gamepass = script:WaitForChild("GamePassid")
local service = game:GetService("GamePassService")

game.Players.PlayerAdded:connect(function(player)
    if service:PlayerHasPass(player,gamepass.Value) then
       local tools = game.ReplicatedStorage:WaitForChild("Tools")
       for i,v in pairs(tools:getChildren()) do
          if v:isA("Tool") then
             v:Clone().Parent = player:WaitForChild("Backpack")
             v:Clone().Parent = player:WaitForChild("StarterGear")
          end
     end
    end
 end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

According to this the function PlayerHasPass is deprecated and shouldn't be used for new work. The recommended course of action is to use another function inside of MarketPlaceService called UserOwnsGamePassAsync. I'll see if I can fix your code for ya with this new function.

local gamepass = script:WaitForChild("GamePassid")
local service = game:GetService("MarketplaceService")

game.Players.PlayerAdded:connect(function(player)
    if service:UserOwnsGamePassAsync(player.UserId,gamepass.Value) then --//This new function takes UserId instead of the player instance
       local tools = game.ReplicatedStorage:WaitForChild("Tools")
       for i,v in pairs(tools:getChildren()) do
          if v:isA("Tool") then
             v:Clone().Parent = player:WaitForChild("Backpack")
             v:Clone().Parent = player:WaitForChild("StarterGear")
          end
     end
    end
 end)

The main difference with these two functions is that the old function used legacy asset id system for gamepasses that isn't used today. If you got your gamepass id from the url it is actually part of the new system of gamepass ids instead of being assets ids.

I hope this helps.

Ad
Log in to vote
-1
Answered by
Robowon1 323 Moderation Voter
4 years ago

1) instead of using PlayerHasPass, use UserOwnsGamePassAsync()

2) use marketplaceservice instead of gamepassservice

3) you need to get player.UserId

4) make it cleaner by getting children before fix:

local gamepass = script:WaitForChild("GamePassid")
local service = game:GetService("MarketPlaceService")

game.Players.PlayerAdded:connect(function(player)
    if service:UserOwnsGamePassAsync(player.UserId,gamepass.Value) then
       local tools = game.ReplicatedStorage:WaitForChild("Tools")
local children = tools:GetChildren()
       for i = 1, #children do
          if children[i]:isA("Tool") then
             local clone = children[i]:Clone()
clone.Parent = player.Backpack
             children[i].Parent = player:WaitForChild("StarterGear")
          end
     end
    end
 end)

Answer this question