Hi, so I’m having trouble with a script. So when you buy the game pass it gives you a gun from ServerStorage.
local Player = game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassId = 17171191
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) then
print("player does not own pass")
local tool = game.ServerStorage["Historic oof gun"]
local klone = tool:clone()
script.Parent.ClickDetector.MouseClick:connect (function(plr)
if klone.Parent ~= plr.StarterPack then
klone.Parent = plr.StarterPack
end
end)
end
But every time i run it on an account that has the game pass I get this message error message in output
Does anyone know what I did wrong?
All help is appreciated (:
LocalPlayer does not work in Server scripts, use the PlayerAdded event of the Player’s service to get a player to get the UserId from, aka, when a player joins, compare if they have the gamepass and if they do ,do the stuff
local tool = game.ServerStorage[“Historic oof gun”]
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassId = 17171191
Players.PlayerAdded:Connect(function(plr)
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, GamepassId) then
print("player owns pass")
local klone = tool:clone()
script.Parent.ClickDetector.MouseClick:connect(function()
if klone.Parent ~= plr.StarterGear then
klone.Parent = plr.StarterGear
end
end)
end
end)
You may need to change the script.Parent to something else I believe, although not sure if you even need it since you can just do this
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local tool = game.ServerStorage["Historic oof gun"]
local GamepassId = 17171191
Players.PlayerAdded:Connect(function(plr)
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, GamepassId) then
print("player owns pass")
tool:Clone().Parent = plr.Backpack
tool:Clone().Parent = plr.StarterGear
end
end)