So I have this simple script that gives you a pickaxe if you own the gamepass, the script works fine if you are in R15. If you are in R6 which is what you are automatically set to in the game, the gamepass doesn’t work. How can I change this gamepass to give it if you are in both R6 or R15?
local MPS = game:GetService("MarketplaceService")
local gamepass_ID = 13117153
local tool = game.ServerStorage["Unobtainable Pickaxe"]
game.Players.PlayerAdded:Connect(function(player)
local has_pass = MPS:UserOwnsGamePassAsync(player.UserId, gamepass_ID)
if has_pass then
local starterGear = player:WaitForChild("StarterGear")
tool:Clone().Parent = starterGear
tool:Clone().Parent = player.Backpack
end
end)
I think the issue is with the tool you are giving. Some tools do not work with R6 avatars. Try seeing if the script gives the tool to the player by checking the player’s backpack or placing a printed statement after the has_pass conditional.
It might be possible that the player is loading in before the script has the chance to connect to the PlayerAdded event. We can combat this by creating a function and calling it for all existing players right after connecting.
function setupPlayer(player)
local has_pass = MPS:UserOwnsGamePassAsync(player.UserId, gamepass_ID)
if has_pass then
local starterGear = player:WaitForChild("StarterGear")
tool:Clone().Parent = starterGear
tool:Clone().Parent = player.Backpack
end
end
game.Players.PlayerAdded:Connect(setupPlayer)
for _, player in pairs(game.Players:GetPlayers()) do
setupPlayer(player)
end
There is a big chance that the player’s backpack isn’t loaded in yet, since you’re only using PlayerAdded which checks for the player instance, solution recommended is to use CharacterAdded.
You could also just do a WaitForChild and wait for the backpack. Actually, it might just be better to see if there is a backpack, and only then add it into the backpack. Once the character loads, the starter-gear will automatically be cloned over to the backpack.
function setupPlayer(player)
local has_pass = MPS:UserOwnsGamePassAsync(player.UserId, gamepass_ID)
if has_pass then
local starterGear = player:WaitForChild("StarterGear")
local backpack = player:FindFirstChild("Backpack")
tool:Clone().Parent = starterGear
if backpack then
tool:Clone().Parent = backpack
end
end
end
game.Players.PlayerAdded:Connect(setupPlayer)
for _, player in pairs(game.Players:GetPlayers()) do
setupPlayer(player)
end
Seems like a solution, but however you’re kind of unneccesarily waiting for an instance when you can run the code as soon as the flag is actually available, also you can just Clone() it to both StarterPack and Backpack, WaitForChild could possibly yield depending on the client.
Roblox has several race conditions in these areas, and they are compounded by differences when run
in studio vs a live game. Using the R6 rig likely just changes the timing just enough to cause your issue.
So not only is there the timing of copying something into StarterGear, which because of the timing
may or may not auto-copy to the Backpack, but the Backpack itself can get replaced (even on an
initial spawn) resulting, in your case with the possibility of 0-2 of those Tools ending up in the Backpack
So as an experiment, try a short delay before you grab and add to the Backpack.