Hello, I prepared a script about game pass. My goal is to copy the gamepass into the backpack if he bought it. but it gives me such an error. Does anyone know the cause or solution
What are you defining oyuncu as?
You don’t need to clone the script, all it will do is use more resources than needed, you can just check this in the normal way:
PlayerAdded > CharacterAdded > Check > Award if true.
EDIT — TL;DR: You need a player instance as a reference to access their character.
PlayerService.PlayerAdded > PlayerInstance.CharacterAdded
At the time of the scripts execution, “oyuncu” is a nil value with nothing assigned to it.
I’m assuming this is what you are trying to accomplish:
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.ChildAdded:Connect(function(Character)
--Your existing code)
end)
end)
I do not recall the OP mentioning anything about cloning the script?
Oyuncu means player in Turkish if that helps.
Sorry, I forgot to post the photo of that place.
Yeah, I agree you can’t call local player in server script
My goal is when I die, if I buy gamepass, the tool in localplayer backpack.
are you turkish? I can explain in turkish if you want.
Based on your code, this should be the solution.
local market = game:GetService("MarketplaceService");
local id = nil; -- change this to the gamepass' id
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if market:UserOwnsGamePassAsync(Player.UserId, id) then
game.ServerStorage.USP:Clone().Parent = Player.Backpack;
end;
end);
end);
local MarketplaceService = game:GetService("MarketplaceService")
local Gamepasses = {
YourId = function(Player)
-- give them whatever
end
} -- fill if with more you want
local function Added(Player)
local UserId = Player.UserId
for GamepassId, Callback in pairs(Gamepasses) do
pcall(function()
if MarketplaceService:UserOwnsGamepassAsync(UserId, GamepassId) then
Callback(Player)
end
end)
end
end
for _, Player in ipairs(game:GetService("Players"):GetPlayers()) do
coroutine.wrap(Added)(Player) -- new event behavior update means we have to do this
end
gamegame:GetService("Players").PlayerAdded:Connect(Added)
changed a few things
Honestly I don’t see why you should complicate the code like this when you can keep it much more simple.
That isn’t complicating it, it’s future-proofing it for an upcoming update and making it so you only have to add like 4 lines if you ever decide to add another gamepass, i’d say it’s engineered enough
You can just copy-paste a UserOwnsGamePassAsync()
block
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if market:UserOwnsGamePassAsync(Player.UserId, id) then
-- gamepass 1
end;
if market:UserOwnsGamePassAsync(Player.UserId, id) then
-- gamepass 2
end;
if market:UserOwnsGamePassAsync(Player.UserId, id) then
-- gamepass 3
end;
end);
end);
Do you not see how counter intuitive that is? Repeating yourself in cases like this is a redundancy. Using a more scalable structure like a table of gamepass → callbacks is going to keep things much cleaner and allow for easy editing of the code without dramatically increasing the code complexity of our main functions (e.g the player added) among other things.
I see. I usually do not create this kind of more “complex” systems so that’s might be why I find it a waste of space, even if logically it isn’t since it’s going to be easier to edit in the future.
you cant use oyuncu because Lua is american. you need English
But you can name your variables whatever you want.