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

How do I make a gamepass tool giver that doesn't need a server restart to work?

Asked by 7 years ago
Edited 7 years ago

So I have a segway in Server Storage that I want to do so if you buy it, you just need to rejoin the server to have it. Cause the gamepass script that I have now needs a server restart before it works. The gamepass Id is : 503854648

Thanks!

[UPDATE] -The script is in Workspace and there is a value in the script with the gamepass id. Thats the code that I have now :

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
    assert(parent, "ERROR: WaitForChild: parent is nil")
    while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
    return parent[childName]
end

-----------------
--| Variables |--
-----------------

local GamePassService = game:GetService('GamePassService')
local PlayersService = game:GetService('Players')
local ServerStorageService = game:GetService('ServerStorage') --TODO: Use new data store service once that exists

local GamePassIdObject = WaitForChild(script, 'GamePassId')
local AdminTools = WaitForChild(ServerStorageService, 'Seg')

-----------------
--| Functions |--
-----------------

-- Makes copies of all the admin tools and puts them in target
local function CloneAdminTools(target)
    for _, tool in pairs(AdminTools:GetChildren()) do
        local toolClone = tool:Clone()
        toolClone.Parent = target
    end
end

-- When a player with the game pass joins, give them the admin tools
local function OnPlayerAdded(player)
    if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
        local starterGear = WaitForChild(player, 'StarterGear')
        CloneAdminTools(starterGear)
        if player.Character then -- They've already loaded and won't get their StarterGear until next spawn
            local backpack = WaitForChild(player, 'Backpack')
            CloneAdminTools(backpack)
        end
    end
end

--------------------
--| Script Logic |--
--------------------

PlayersService.PlayerAdded:connect(OnPlayerAdded)

0
You don't need to rewrite the WaitForChild function. If you don't want to use the method syntax, just localize it by: local WaitForChild = game.WaitForChild Validark 1580 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Hey,

The problem is the PlayerAdded event. You're only checking it once. What I would do is when the player joins, run a loop (every 15 seconds) and call the function "OnPlayerAdded" with the player's name of course. So, in the end:

local function OnPlayerAdded(player)
    if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
        local starterGear = WaitForChild(player, 'StarterGear')
        CloneAdminTools(starterGear)
        if player.Character then -- They've already loaded and won't get their StarterGear until next spawn
            local backpack = WaitForChild(player, 'Backpack')
            CloneAdminTools(backpack)
        end
    return true
    end
end

game.Players.PlayerAdded:connect(function(p)
while wait(15) do
if OnPlayerAdded(p) then
break
end
end
end)

Another way you can do is make a gui that when clicked, checks if the player has the gamepass or not (you should set a time frame between each "check" so the players wont spam the button).

Ad

Answer this question