Hey Developers!
I am making a story game, I’ve nearly finished. The only part left, is to make the shop gui. I did that fine, and I got to the point where I needed to redeem the items in game. Recently, Roblox removed the thing where you automatically owned Group gamepasses when you made them. So I had to test it like this:
local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')
PlayerService.PlayerAdded:Connect(function(Player)
print('New Player')
if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:WaitForChild('Backpack')
else
print('Doesnt Own')
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:WaitForChild('Backpack')
end
end)
Doesn’t Own is printing, but the player isn’t getting the item, I get no errors.
Any help is appreciated!
The player is supposed to get the item, even if they don’t own the Game Pass. The reason they coded like this, is to test it, since they don’t have the Game Pass. They’re not getting the item, is the issue.
If the instance cannot be found, the script will yield and won’t give the item. Additionally, you could add a timeOut parameter to :WaitForChild(). Do this by adding a second parameter to :WaitForChild().
Example: :WaitForChild(Instance, timeOut)
I’ve just compared your code to mine, and I’ve figured out your problem. I replaced :WaitForChild() with :FindFirstChild(), when looking for the players backpack.
Edit: I forgot to add Player:WaitForChild(“Backpack”) in the beginning of the function, excuse me!
local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')
PlayerService.PlayerAdded:Connect(function(Player)
Player:WaitForChild('Backpack')
print('New Player')
if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
else
print('Doesnt Own')
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
end
end)
Using Darky’s method, the player will get the item whenever they spawn.
Here’s how your code should look like with this method;
local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')
PlayerService.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
Player:WaitForChild('Backpack')
print('New Player')
if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
else
print('Doesnt Own')
game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
end
end)
end)
PlayerAdded function runs before the character spawns, it could happen while the Player is still loading to the game. Character added runs when the Player has finally loaded in the game. This is from my knowledge.
CharacterAdded runs when the player spawns. If you set game.Players.CharacterAutoLoads to false, then you will have to manually spawn the player. This is useful for action games, like Phantom Forces, where you click spawn to load into the map.
Thanks for all your help. @Darkyv_v fixed it with the Character Added Function.
I ended up using this script, and it works perfectly.
local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')
PlayerService.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
print('New Player')
if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
local Rod = game.ServerStorage.Assets.FlamingRod:Clone()
Rod.Parent = Player.Backpack
else
local Rod = game.ServerStorage.Assets.FlamingRod:Clone()
Rod.Parent = Player.Backpack
end
if MPS:UserOwnsGamePassAsync(Player.UserId, 8956414) then
local Flashlight = game.ServerStorage.Assets.Flashlight:Clone()
Flashlight.Parent = Player.Backpack
else
local Flashlight = game.ServerStorage.Assets.Flashlight:Clone()
Flashlight.Parent = Player.Backpack
end
if MPS:UserOwnsGamePassAsync(Player.UserId, 8956681) then
local Medkit = game.ServerStorage.Assets.FirstAid:Clone()
Medkit.Parent = Player.Backpack
else
local Medkit = game.ServerStorage.Assets.FirstAid:Clone()
Medkit.Parent = Player.Backpack
end
end)
end)