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

I need help with creating a function loading the player's inventory?

Asked by 5 years ago

Hello there,

I've tried in multiple different ways to do the same thing. It just won't work, is there some obvious mistake I've just missed? I'm simply trying to make a function that when a user dies (or if chosen with a server event) load the users inventory. When loading the users inventory it would then check if and what gamepasses the user has. If the user has VIP, then it should not check for the other passes, but if not it should. I'm stuck.

Current Code

local mps = game:GetService("MarketplaceService")

local supercoil = game.ReplicatedStorage.SuperCoil
local supercoilid = 5892398
local HasSuperCoil = false

local grapplehook = game.ReplicatedStorage.GrappleHook
local grapplehookid = 5892399
local HasGrappleHook = false

local gravitycoil = game.ReplicatedStorage.GravityCoil
local gravitycoilid = 5892400
local HasGravityCoil = false

local speedcoil = game.ReplicatedStorage.SpeedCoil
local speedcoilid = 5892401
local HasSpeedCoil = false

local VIP = 5892397

--- Check if and what gamepasses user has.

function CheckVIP()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            if mps:UserOwnsGamePassAsync(player.UserId,VIP) then
                print(player.Name .. " has the VIP gamepass!")
                local clone = game.ReplicatedStorage.GrappleHook:Clone()
                clone.Parent = player:WaitForChild("Backpack")
                local clone = game.ReplicatedStorage.SuperCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
                local clone = game.ReplicatedStorage.GravityCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
                local clone = game.ReplicatedStorage.SpeedCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
            else
                print(player.Name .. " does not have the VIP gamepass...")
            end
        end)
    end)
end

function CheckGrappleHook()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            if mps:UserOwnsGamePassAsync(player.UserId,grapplehookid) then
                print(player.Name .. " has the grapple hook gamepass!")
                local clone = game.ReplicatedStorage.GrappleHook:Clone()
                clone.Parent = player:WaitForChild("Backpack")
            else
                print(player.Name .. " does not have the grapple hook gamepass...")
            end
        end)
    end)
end

function CheckSuperCoil()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            if mps:UserOwnsGamePassAsync(player.UserId,supercoilid) then
                print(player.Name .. " has the Super Coil gamepass!")
                local clone = game.ReplicatedStorage.SuperCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
            else
                print(player.Name .. " does not have the Super Coil gamepass...")
            end
        end)
    end)
end

function CheckSpeedCoil()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            if mps:UserOwnsGamePassAsync(player.UserId,speedcoilid) then
                print(player.Name .. " has the Speed Coil gamepass!")
                local clone = game.ReplicatedStorage.SpeedCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
            else
                print(player.Name .. " does not have the Speed Coil gamepass...")
            end
        end)
    end)
end

function CheckGravityCoil()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            if mps:UserOwnsGamePassAsync(player.UserId,gravitycoilid) then
                print(player.Name .. " has the Gravity Coil gamepass!")
                local clone = game.ReplicatedStorage.GravityCoil:Clone()
                clone.Parent = player:WaitForChild("Backpack")
            else
                print(player.Name .. " does not have the Gravity Coil gamepass...")
            end
        end)
    end)
end

function LoadInventory()
    game:GetService("Players").PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            player:WaitForChild("Backpack")
            if mps:UserOwnsGamePassAsync(player.UserId,5892397) then
                print("The script won't go further than here.")
                CheckVIP()
            else
                CheckGrappleHook()
                CheckSuperCoil()
                CheckSpeedCoil()
                CheckGravityCoil()
            end
        end)
    end)
end

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        LoadInventory()
        print(player.Name .. "'s inventory has been loaded.")
    end)
end)

LoadInventory()

1 answer

Log in to vote
0
Answered by 5 years ago

General Practice

Use :GetService() to retrieve the ReplicatedStorage

Use :WaitForChild() to make sure something exists before using it

If you are going to name local variables, I suggest using them within the code, since otherwise there's no point in having them.

Issues

Do not use a Local Variable with the same name as another local variable

You are adding PlayerAdded events within the LoadInventory function, however, its successor function already uses this, so what you are doing is waiting for a player to join after the first player has joined, before connecting the function.

You can also pass the player instance from your initial connection using the provided parameters of any of your local functions, this includes the player's backpack.

Revised Server Script

local mps = game:GetService("MarketplaceService")
local VIP = 5892397

local supercoil = game:GetService("ReplicatedStorage"):WaitForChild("SuperCoil")
local supercoilid = 5892398
local HasSuperCoil = false

local grapplehook = game:GetService("ReplicatedStorage"):WaitForChild("GrappleHook")
local grapplehookid = 5892399
local HasGrappleHook = false

local gravitycoil = game:GetService("ReplicatedStorage"):WaitForChild("GravityCoil")
local gravitycoilid = 5892400
local HasGravityCoil = false

local speedcoil = game:GetService("ReplicatedStorage"):WaitForChild("SpeedCoil")
local speedcoilid = 5892401
local HasSpeedCoil = false

local function CheckVIP(player, backpack)
    if mps:UserOwnsGamePassAsync(player.UserId, VIP) then
        print(player.Name .. " has the VIP gamepass!")
        local clone1 = grapplehook:Clone()
        clone1.Parent = backpack
        local clone2 = supercoil:Clone()
        clone2.Parent = backpack
        local clone3 = gravitycoil:Clone()
        clone3.Parent = backpack
        local clone4 = speedcoil:Clone()
        clone4.Parent = backpack
    else
        print(player.Name .. " does not have the VIP gamepass...")
    end
end

local function CheckGrappleHook(player, backpack)
    if mps:UserOwnsGamePassAsync(player.UserId,grapplehookid) then
        print(player.Name .. " has the grapple hook gamepass!")
        local clone = grapplehook:Clone()
        clone.Parent = backpack
    else
        print(player.Name .. " does not have the grapple hook gamepass...")
    end
end

local function CheckSuperCoil(player, backpack)
    if mps:UserOwnsGamePassAsync(player.UserId, supercoilid) then
        print(player.Name .. " has the Super Coil gamepass!")
        local clone = supercoil:Clone()
        clone.Parent = backpack
    else
        print(player.Name .. " does not have the Super Coil gamepass...")
    end
end

local function CheckSpeedCoil(player, backpack)
    if mps:UserOwnsGamePassAsync(player.UserId, speedcoilid) then
        print(player.Name .. " has the Speed Coil gamepass!")
        local clone = speedcoil:Clone()
        clone.Parent = backpack
    else
        print(player.Name .. " does not have the Speed Coil gamepass...")
    end
end

local function CheckGravityCoil(player, backpack)
    if mps:UserOwnsGamePassAsync(player.UserId, gravitycoilid) then
        print(player.Name .. " has the Gravity Coil gamepass!")
        local clone = gravitycoil:Clone()
        clone.Parent = backpack
    else
        print(player.Name .. " does not have the Gravity Coil gamepass...")
    end
end

local function LoadInventory(player)
    local backpack = player:WaitForChild("Backpack")
    if mps:UserOwnsGamePassAsync(player.UserId, VIP) then
        print("The script won't go further than here.")
        CheckVIP(player, backpack)
    else
        CheckGrappleHook(player, backpack)
        CheckSuperCoil(player, backpack)
        CheckSpeedCoil(player, backpack)
        CheckGravityCoil(player, backpack)
    end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        LoadInventory(player)
        print(player.Name .. "'s inventory has been loaded.")
    end)
end)
0
Thank you very much for the help! You spent a lot of time explaining this and I am very thankful! Similaritea 58 — 5y
Ad

Answer this question