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

Scripting Help When Player Enters Game?

Asked by 7 years ago
Edited 7 years ago

Hey I need this script to work when the player enters the game but its only working when they respawn. How to I get it to work when they enter. I have tried a couple different things but its not working. Any help is appreciated. Thank you

GPS = game:GetService("GamePassService")
function respawned(char)
local player = game.Players:FindFirstChild(char.Name)
print("Respawned")
if char:FindFirstChild("Head") ~= nil then
print("It's a 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)


Revised code

GPS = game:GetService("GamePassService")
game.Players.PlayerAdded:connect( function(player)
    print("it worked")
local player = game.Players:FindFirstChild(player.Name)
player.CharacterAdded:connect(function(character)
print("Respawned")
if player:FindFirstChild("Head") ~= nil then
print("It's a Player!")
if GPS:PlayerHasPass(player, gpid) then
print("Has GPID")
for i = 1,#tools do
game.ServerStorage.GamepassWeapons:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
end
else
print("No GPID")
end
end
end)
end)

0
try using game.Players.PlayerAdded:Connect(respawned) oSyM8V3N 429 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You are using ChildAdded on the workspace, thus meaning that it will fire every time something new is added to the workspace, what you are after is PlayerAdded, this will fire when a new player joins the game. Based on what your usage seems to be, you will most likely need to use CharacterAdded after that on the player parameter that is passed back from PlayerAdded, this will allow you to run your code every time the character spawns.

EDIT: This can also be solved by using the player's StarterGear too, I have also written code to show you the approach that I would personally take on this matter. The 'gpid' variable in both attempts are you code above is invalid and likely part of the problem you've encountered.

local GPS       =   game:GetService("GamePassService");
local tools     =   game:GetService("ServerStorage"):FindFirstChild("GamepassWeapons");
local gpid      =   0000; -- Enter your gamepass id here;

game:GetService("Players").PlayerAdded:Connect(function(player)
    local starterGear   =   player:WaitForChild("StarterGear");
    local backpack      =   player:WaitForChild("Backpack");

    if (starterGear and backpack and tools) then
        local success, response = pcall(function()
            return GPS:PlayerHasPass(player, gpid);
        end)

        if (success) then
            if (response) then -- If the player has the gamepass;
                print(string.format("Player %s has gamepass %s.", player.Name, tostring(gpid));

                for _, v in pairs(tools:GetChildren()) do
                    local sgClone   =   v:Clone();
                    sgClone.Parent  =   starterGear;

                    local bpClone   =   v:Clone();
                    bpClone.Parent  =   backpack;
                end
            end
        else
            warn("[GPS] Could not check for player pass. \n Response: " .. response);
        end
    end
end)

When using a call to a web service such as GamePassService, it is a good idea to wrap it in a pcall() to ensure that your code is able to deal with it in whatever way you like if there is an issue accessing the web server, especially seeing as you can output the error as can be seen above so you can give yourself an even more accurate breakdown of the issue.

Another useful function used in the above section is a standard library iterator, that is the section that looks similar to for i, v in pairs() end. This is useful as it allows you to directly loop through the table returned by GetChildren() which returns a table filled with the contents of the directory you called it on.

0
I tried revising the code in a bunch of different ways. I added the revised code above but to no avail. Also I am not getting any error messages CrazyRoblots 13 — 7y
0
I have modified my answer to include an example of the sort of approach that I would take and explained it, if you have any further questions/issues then feel free to ask. RaverKiller 668 — 7y
0
Thanks so much for the help. I now have the script working thanks to the answers provided CrazyRoblots 13 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
game.Players.PlayerAdded:connect(function(Player) --Player is the player object that joined the game
    Player.CharacterAdded:connect(function(Character) --Character is the character of the player
    end)
end)

Answer this question