No, because all of those functions are firing, again, before the character is added. Here’s what you should be doing:
local marketPlaceService = game:GetService('MarketPlaceService')
local serverStorage = game:GetService('ServerStorage')
local players = game:GetService('Players')
local gamepassId = 19711172
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local success, hasPass = pcall(function()
return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if success then
if hasPass then
serverStorage.Gravitycoil:Clone().Parent = player:WaitForChild('Backpack')
serverStorage.Gravitycoil:Clone().Parent = player:WaitForChild('StarterGear')
end
else
warn(hasPass)
end
end)
end)
local marketPlaceService = game:GetService('MarketPlaceService')
local serverStorage = game:GetService('ServerStorage')
local players = game:GetService('Players')
local gamepassId = 19711172
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local success, hasPass = pcall(function()
return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if success then
if hasPass then
print("has pass")
print(hasPass)
serverStorage.Gravitycoil:Clone().Parent = player:WaitForChild('Backpack')
serverStorage.Gravitycoil:Clone().Parent = player:WaitForChild('StarterGear')
end
else
warn(hasPass)
end
end)
end)
local marketPlaceService = game:GetService('MarketPlaceService')
local serverStorage = game:GetService('ServerStorage')
local players = game:GetService('Players')
local gamepassId = 19711172
players.PlayerAdded:Connect(function(player)
local success, hasPass = pcall(function()
return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if success then
if hasPass then
player.CharacterAdded:Connect(function()
serverStorage.Gravitycoil:Clone().Parent = player:WaitForChild('Backpack')
end)
end
else
warn(hasPass)
end
end)
I also noticed that you forgot to add game:GetService(“MarketplaceService”) in the if statement
local GamePassId = 19711172
game.Players.PlayerAdded:Connect(function(player)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, GamePassId) then
game.ServerStorage.Gravitycoil:Clone().Parent = player.Backpack
game.ServerStorage.Gravitycoil:Clone().Parent = player.StarterGear
end
end)
Can I see the location of everything necessary? All the trees of every instance location? Like screenshots of where the coil is located, and the scripts?
The code you’re running is not part of the event, put it inside the PlayerAdded function.
local GamePassId = 19711172
game.Players.PlayerAdded:Connect(function(player)
if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassId) then
game.ServerStorage.Gravitycoil:Clone().Parent = player:WaitForChild("Backpack")
game.ServerStorage.Gravitycoil:Clone().Parent = player:WaitForChild("StarterGear")
end
end)
Try using this code. You place the tool you want to give inside the script, replace ‘gamepassid’ with your passid and replace script.tool with the tool name.
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassId = 1234567 -- REPLACE 1234567 WITH YOUR GAMEPASS ID
local Tool = script.GravityCoil -- REPLACE GravityCoil WITH THE NAME OF YOUR TOOL
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) then
local ToolClone = Tool:Clone()
ToolClone.Parent = Player.Backpack
end
end)
end)
Dukzae is right over here, the tool should be cloned into the backpack each time the player spawns.
Do not place your tools in ReplicatedStorage. The clients have access to this, and can clone it on their own to use.
I looked over your code, and noticed some spelling problems, case sensitivity problems, and efficiency problems that I can help with. One thing to note is “UserOwnsGamePassAsync” is an expensive function, and you really don’t want to be calling it each time the character spawns. That’s why you should only call it once when the player joins, and depending on whether they own it or not, clone the tool when they spawn.
-- Services and Variables
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game:GetService("ServerStorage")
local GAMEPASS_ID = 19711172
-- A cache to index if a player owns the gamepass when they join
local cache = {}
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Repeats until ownership is detected
if cache[player] == nil then
repeat
wait()
until
cache[player] ~= nil
end
-- If the player owns the gamepass, clone the gravity coil into their backpack
local ownsGamepasses = cache[player]
if ownsGamepasses then
ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
end
end)
-- This runs ONLY ONCE because UserOwnsGamePassAsync is an expensive function
-- IT shouldn't be called every time the player spawns, so ownership will be indexed in the cache
local success, hasPass = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAMEPASS_ID)
end)
if success then
cache[player] = hasPass
else
warn("Failed to retrieve ownership data: " .. player.Name)
cache[player] = false
end
end)