I'm testing out a gamepass that gives the player a potato when they buy it.
Currently, it's not working because it says player is nil at line 13.
How can I fix this?
local passId = 264705769 -- change this to your game pass ID. local potato = game.Lighting:WaitForChild("Potato"):Clone() function isAuthenticated(player) -- checks to see if the player owns your pass return game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) end game.Players.PlayerAdded:connect(function(plr) if isAuthenticated(plr) then local player = game.Players.LocalPlayer print(plr.Name .. " has bought the game pass with id " .. passId) potato.Parent = player.Backpack potato.Parent = player.StarterGear end end)
You don't need and you can't declare the player such as "local player = game.Player.LocalPlayer" when you're on a Server Script.And you don't need to declare your player two time, they're the same player.
Your script fix here:
local passId = 264705769 -- change this to your game pass ID. local potato = game.Lighting:WaitForChild("Potato"):Clone() function isAuthenticated(player) -- checks to see if the player owns your pass return game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) end game.Players.PlayerAdded:connect(function(plr) if isAuthenticated(plr) then print(plr.Name .. " has bought the game pass with id " .. passId) potato.Parent = plr.Backpack potato.Parent = plr.StarterGear end end)
LocalPlayer
only works if using a LocalScript, The player is already defined by the parameter plr
.
local passId = 264705769 local potato = game.Lighting:WaitForChild("Potato"):Clone() function isAuthenticated(player) return game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) end game.Players.PlayerAdded:connect(function(plr) -- player is already defined if isAuthenticated(plr) then print(plr.Name .. " has bought the game pass with id " .. passId) potato.Parent = plr.Backpack potato.Parent = plr.StarterGear end end)
local passId = 264705769 local potato = game.Lighting:WaitForChild("Potato"):Clone()
function isAuthenticated(player) return game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) end
game.Players.PlayerAdded:connect(function(plr) -- player is already defined if isAuthenticated(plr) then print(plr.Name .. " has bought the game pass with id " .. passId) potato.Parent = plr.Backpack potato.Parent = plr.StarterGear end end)