How do I revise this script so that it checks if the player has the gamepass everytime they respawn instead of when they join?

Title says all, any help would be greatly appreciated!
(Script was not written by me)

local MarketPlaceService = game:GetService("MarketplaceService")
 
local GamepassId = 55244710
local Tool = script.GravityCoil
 
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)

This does check for the gamespass every time you respawn. Also, what I usually do is check if they have the gamepass right after they buy, if they do give the special thingy, that way all they have to do is buy it and nothing else.

I’ve tested with this script ingame and it only works after rejoining because of this part here but I’m not sure how exactly to fix it. It’s a problem because people have to rejoin the game after buying a pass in order for it to work.

Is this a LocalScript, or a server script? (just to be sure)

Server


The current issue that it doesnt detect purchased gamepass once the player respawns, am i right?

Yes, to clarify that means that the player has to rejoin the game in order to receive the gamepass every time they spawn.

The event CharacterAdded only gets called when the player joins, and I think this might be the issue.
I have an idea that uses RemoteEvents, but I assume you dont want to use it, since they are exploitable.
Let me know if you want to use only the server script, or by 2 scripts(LocalScript → RemoteEvent → server script)

That is false. Even the documentation states "Player.CharacterAdded runs when a player’s character spawn (or respawns)"

Anything’s fine really, as long as the script functions

I’m saying that the line Player.CharacterAdded:Connect(function(Character) comes right after game.Players.PlayerAdded:Connect(function(Player), which checks for a player getting added; i meant that it only checks once(only when the player gets added), not everytime a player respawns

This is the solution. I think you should mark it. It already does check if the gamepass is bought every time the player respawns.

The script doesn’t work because you have to rejoin the game for the “gamepass on respawn” to work

Do it for both …

function Spawned(player)
-- your code here
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		Spawned(player)
	end)
end)

And you may want to add one more right after they buy it in game …

Make characterAdded a function and wait for the player’s character manually. Call the function then. After that, connect it to CharacterAdded.

I will try this rq


How would I go about doing that?

-- client side code, can be in StarterGui or smth
local RE = game:GetService("ReplicatedStorage").RemoteEvent -- going with the remoteevent way
local gamepass = -- gamepass here
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	  RE:FireServer(gamepass)
end)

-- serverside code

local MarketPlaceService = game:GetService("MarketplaceService")
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(player, gamepass)
    if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
       local ToolClone = Tool:Clone()
       ToolClone.Parent = Player.Backpack
    end
end)

Have you tried resetting after purchasing the gamepass? You should be awarded the gravity coil then (without needing to leave and rejoin).

1 Like

Add a function called CharacterAdded outside of PlayerAdded (or in a local function). Then, call it after 2.5 seconds after the player joins. Connect it up to CharacterAdded then you’re done.