Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Will this work for giving a character tools if they have a gamepass?

Asked by
Akozta 40
9 years ago
wait(2)
gpid = 1 -- Gamepass ID here.
tools = {"tool1", "tool2", "tool3"} -- Put the tools in Lighting, and their names here.
GPS = Game:GetService("GamePassService")
function respawned(char)
player = game.Players:FindFirstChild(char.Name)
print("Respawned")
if char:FindFirstChild("Head") ~= nil then
print("Player")
if GPS:PlayerHasPass(player, gpid) then
print("Has GPID")
for i = 1,#tools do
game.Lighting:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
end
else
print("No GPID")
end
end
end
game.Workspace.ChildAdded:connect(respawned)

0
I'm a beginner at scripting Akozta 40 — 9y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

In the future if you just want to know if something will work, just test it rather than coming here

This would work only if the Child that entered the workspace was a player - otherwise it would error.

To make this error-proof, use a PlayerAdded event with a CharacterAdded event.

Also, I suggest you use the PlayerOwnsAsset method, as it returns live results from the website while the PlayerHasPass method returns cached results - meaning that you'd have to rejoin the game if you were to buy the gamepass in game.

Additionally, Lighting shouldn't be used for storage - I suggest using ServerStorage.

This script should be in workspace;

--Put your gamepass ID here
local assetId = 000000
-- Put the tools in ServerStorage, and their names here.
local tools = {"tool1", "tool2", "tool3"}
local ms = game:GetService('MarketplaceService')

function giveTools(plr)
    if ms:PlayerOwnsAsset(plr,assetId) then
        for i = 1,#tools do
            local tool = game.ServerStorage:FindFirstChild(tools[i])
            if tool then
                if not plr.Backpack:FindFirstChild(tool.Name) then
                    tool:Clone().Parent = plr.Backpack
                end
            end
        end
    end
end

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(char)
        giveTools(plr)
    end)
end)
0
Thanks for the help, the only reason I didn't test it out was because I didn't want to purchase the gamepass(for my game) if I didn't know how to give people perks yet. Akozta 40 — 9y
0
You don't have to purchase a gamepass that is created by you. You automatically own it, silly. Same goes for Developer Products. Goulstem 8144 — 9y
Ad

Answer this question