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

why does the script work in solo but not in play mode ?

Asked by 9 years ago

the script is in the Workspace

game.Players.ChildAdded:connect (function ()
    print'loading inventory items'
local valueListF = {"130213380","110336757","128992838","8329686","23932048","150182466","19264845","20722130","7074836","13038375","116042990","21024661","30394594","10907541","119812659","16101765","31117267","7076076","20418682","7131361","110207471","141728790","7074944","34764447","51241480","134522901","23644960","20418658","110288809","51241522","22877700","8560971","26424808","8560975","10860397","8329679","16132514","162200696","21311601","15432080","7074864","7699177","66330295","26619096","173789324","30394484","10907549","23264604","19398554","162068415","27599954","10907529","18151826","7074893","21635565","119812738","33321848","7075434","7131886","23219981","45084008","14861743","162200633","29109681","32058239","10527010","14030577","20612949","19366445","20010377","15637848","10907551","23261769","150182501","20337343","133360789","12732366","7317765","21635583","66330106","22828351","119812715","12188176","12145328","12467159","10860384","15885121","162200666","32873408","10770432","28999228","7075469","29532363","66330060","35168581","10831454","15324577","19396123","21802396","147144644","49045351","173789114","14516578","30394850","34673712","10831558","15054426","22500129","159199178","14812981","12145366","11389441","15471035","26260927","20644021","45515121","51241171","27861352","20909103","32723404","27134344","27725434","29348122","7076224","7317793"
}
local valueList = {
"172309919", "1029025", "135470963", "188664226", "190786130", "190786046", "191098660", "60115635", "151784320", "16630147","188702967", "190204972", "17424092", "1563352","133553855", "18757806", "13745548", "17877340", "39247441", "188703294", "1073690","170892848", "172309861", "60675871", "14815761", "13476917", "15967743", "29952810","140469731", "164482409", "63994465", "15913848", "185812297", "187846561", "11188696", "139618317","48039359","62246484","125369932","68325607","175136000","47702745","67200948","163524136","188703010","135470710","62745990","185893592","1374269","63689493","168167316","153955396","99860652","136791461","81722657","153955895","139589487","136790950","46302558","64444871","68258723","1286490","128159108","1158416","96103379","20573086","39247498","52554739","29051524","23705521","22546563","42211680","1285307","1235488","162067148","1323384","135470997","1272714","138932314","63043890","21070012","11748356","20980138","188888052","72082328","31101391","48545806","100929604"
}
local valueListC = {"169716851","169717184" 
}
ply = game.Players.LocalPlayer
function getMyPack()
    return game.Lighting:FindFirstChild(ply.Name .. "sPack")
end
local pack = getMyPack()

for i = 1, #valueList do
    wait(0.1)
    local n = script.Parent.Value:Clone()
    local Asset = game:GetService("MarketplaceService"):GetProductInfo(valueList[i])
    n.Name = Asset.Name
    n.Value = valueList[i]
    n.Parent = pack.holder
end

for i = 1, #valueListF do
    wait(0.1)
    local n = script.Parent.Value:Clone()
    local Asset = game:GetService("MarketplaceService"):GetProductInfo(valueListF[i])
    n.Name = Asset.Name
    n.Value = valueListF[i]
    n.Parent = pack.faces

end
local name = "Quad"
local message = {ply.PlayerGui.Catalog.Frame1.TextLabel,ply.PlayerGui.Catalog.Frame1.hat,ply.PlayerGui.Catalog.Frame1.button}
local visible = false


function list1()
for i = 1,#message do
    if visible == false then
        message[i].Visible = false
    elseif visible == true then
        message[i].Visible = true
    end
end
end

list1()
for i = 1, #valueListC do
    wait(0.1)
    local n = script.Parent.Value:Clone()
    n.Value = valueListC[i]
    local Asset = game:GetService("MarketplaceService"):GetProductInfo(valueListC[i])
    n.Name = Asset.Name
    n.Parent = pack.clothes
    end
print'finished!!'
script.Parent.check.Value = 0
    ply.PlayerGui.Catalog.Frame1.hat.Text = "Inventory items have been loaded!"  
    ply.PlayerGui.Catalog.Frame1:TweenSizeAndPosition(UDim2.new(0.25, 0, 0.3, 0), UDim2.new(0.4, 0, 0.45, 0), "Out", name, 1, false)
    visible = true
    list1() 
end)

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Only LocalScripts can use LocalPlayer, but a LocalScript won't run in the Workspace. It will only run in a player's PlayerGui or their Character. Since the ChildAdded gives you the player that just joined, there's absolutely no reason for you to use LocalPlayer anyway.


This whole script is formatted very poorly...


Indent reasonably, and don't have lines that are 1000+ characters. Break them up.


You're defining a global function inside of another function for no good reason.


You're using for i = .... loops even though you're only caring about the elements of a list.


Lists (e.g., message) generally should have a plural names, so that you can do things like

for _, message in pairs(messages) do

and make it read like the intuitive meaning!


Don't call functions without parenthesis, and be consistent with your strings! Don't use single quote strings when you use double quote strings everywhere else.


Names must mean something. list1() is a terrible name.


Don't make functions use variables outside of the function when it's not necessary -- use parameters, since that's what you mean! (e.g., the variable visible doesn't serve any purpose as a variable)


When you are typing the same code out (the loops dealing with the valueLists) repeatedly, that's a hint you should write a function to do that thing.


Put relevant code near relevant code. Don't make yourself scroll up and down.

A cleaned up version

function getPack(player)
    return game.Lighting:FindFirstChild(player.Name .. "sPack")
    -- Since you don't do error checking, you might as well use
    -- return game.Lighting[player.Name .. "sPack"]
end

function setMessageVisibility(objects, visible)
    for _, object in pairs(objects) do
        object.Visible = visible -- No need for `if` since this is equivalent
    end
end

function loadValues(values, parent)
    for _, value in ipairs(values) do
        wait(0.1)
        local Asset = game:GetService("MarketplaceService"):GetProductInfo(value)
        local n = script.Parent.Value:Clone()
        n.Name = Asset.Name
        n.Value = value
        n.Parent = parent
    end
end

function newPlayer(ply)
    print'loading inventory items'
    local valueListF = {
        130213380,110336757,128992838,8329686,23932048,150182466,19264845,20722130,7074836,13038375,116042990,21024661,
        30394594,10907541,119812659,16101765,31117267,7076076,20418682,7131361,110207471,141728790,7074944,34764447,
        51241480,134522901,23644960,20418658,110288809,51241522,22877700,8560971,26424808,8560975,10860397,8329679,
        16132514,162200696,21311601,15432080,7074864,7699177,66330295,26619096,173789324,30394484,10907549,23264604,
        19398554,162068415,27599954,10907529,18151826,7074893,21635565,119812738,33321848,7075434,7131886,23219981,
        45084008,14861743,162200633,29109681,32058239,10527010,14030577,20612949,19366445,20010377,15637848,10907551,
        23261769,150182501,20337343,133360789,12732366,7317765,21635583,66330106,22828351,119812715,12188176,12145328,
        12467159,10860384,15885121,162200666,32873408,10770432,28999228,7075469,29532363,66330060,35168581,10831454,
        15324577,19396123,21802396,147144644,49045351,173789114,14516578,30394850,34673712,10831558,15054426,22500129,
        159199178,14812981,12145366,11389441,15471035,26260927,20644021,45515121,51241171,27861352,20909103,32723404,
        27134344,27725434,29348122,7076224,7317793}
    local valueList = {
        172309919, 1029025, 135470963, 188664226, 190786130, 190786046, 191098660, 60115635, 151784320, 16630147,
        188702967, 190204972, 17424092, 1563352,133553855, 18757806, 13745548, 17877340, 39247441, 188703294, 1073690,
        170892848, 172309861, 60675871, 14815761, 13476917, 15967743, 29952810,140469731, 164482409, 63994465, 15913848,
        185812297, 187846561, 11188696, 139618317,48039359,62246484,125369932,68325607,175136000,47702745,67200948,
        163524136,188703010,135470710,62745990,185893592,1374269,63689493,168167316,153955396,99860652,136791461,
        81722657,153955895,139589487,136790950,46302558,64444871,68258723,1286490,128159108,1158416,96103379,20573086,
        39247498,52554739,29051524,23705521,22546563,42211680,1285307,1235488,162067148,1323384,135470997,1272714,
        138932314,63043890,21070012,11748356,20980138,188888052,72082328,31101391,48545806,100929604}
    local valueListC = {169716851, 169717184}

    local pack = getMyPack(ply)

    local messages = {
        ply.PlayerGui.Catalog.Frame1.TextLabel,
        ply.PlayerGui.Catalog.Frame1.hat,
        ply.PlayerGui.Catalog.Frame1.button}

    setMessageVisibility(messages, false)

    loadValues(valueList, pack.holder)
    loadValues(valueListF, pack.faces)
    loadValues(valueListC, pack.clothes)

    print("finished!!")

    script.Parent.check.Value = 0
    ply.PlayerGui.Catalog.Frame1.hat.Text = "Inventory items have been loaded!"

    local name = "Quad"

    ply.PlayerGui.Catalog.Frame1:TweenSizeAndPosition(
        UDim2.new(0.25, 0, 0.3, 0), UDim2.new(0.4, 0, 0.45, 0), "Out", name, 1, false)

    setMessageVisibility(messages, true) 
end

game.Players.PlayerAdded:connect(newPlayer)

Since this doesn't use LocalPlayer, there shouldn't be problems.

Ad
Log in to vote
0
Answered by 9 years ago

this works in studio but not play mode again



function setMessageVisibility(objects, visible) for _, object in pairs(objects) do object.Visible = visible -- No need for `if` since this is equivalent end end function loadValues(values, parent) for _, value in ipairs(values) do wait(0.1) local Asset = game:GetService("MarketplaceService"):GetProductInfo(value) local n = script.Parent.Value:Clone() n.Name = Asset.Name n.Value = value n.Parent = parent end end function newPlayer(ply) print'loading inventory items' local valueListF = {"130213380","110336757","128992838","8329686","23932048","150182466","19264845","20722130","7074836","13038375","116042990","21024661","30394594","10907541","119812659","16101765","31117267","7076076","20418682","7131361","110207471","141728790","7074944","34764447","51241480","134522901","23644960","20418658","110288809","51241522","22877700","8560971","26424808","8560975","10860397","8329679","16132514","162200696","21311601","15432080","7074864","7699177","66330295","26619096","173789324","30394484","10907549","23264604","19398554","162068415","27599954","10907529","18151826","7074893","21635565","119812738","33321848","7075434","7131886","23219981","45084008","14861743","162200633","29109681","32058239","10527010","14030577","20612949","19366445","20010377","15637848","10907551","23261769","150182501","20337343","133360789","12732366","7317765","21635583","66330106","22828351","119812715","12188176","12145328","12467159","10860384","15885121","162200666","32873408","10770432","28999228","7075469","29532363","66330060","35168581","10831454","15324577","19396123","21802396","147144644","49045351","173789114","14516578","30394850","34673712","10831558","15054426","22500129","159199178","14812981","12145366","11389441","15471035","26260927","20644021","45515121","51241171","27861352","20909103","32723404","27134344","27725434","29348122","7076224","7317793" } local valueList = { "172309919", "1029025", "135470963", "188664226", "190786130", "190786046", "191098660", "60115635", "151784320", "16630147","188702967", "190204972", "17424092", "1563352","133553855", "18757806", "13745548", "17877340", "39247441", "188703294", "1073690","170892848", "172309861", "60675871", "14815761", "13476917", "15967743", "29952810","140469731", "164482409", "63994465", "15913848", "185812297", "187846561", "11188696", "139618317","48039359","62246484","125369932","68325607","175136000","47702745","67200948","163524136","188703010","135470710","62745990","185893592","1374269","63689493","168167316","153955396","99860652","136791461","81722657","153955895","139589487","136790950","46302558","64444871","68258723","1286490","128159108","1158416","96103379","20573086","39247498","52554739","29051524","23705521","22546563","42211680","1285307","1235488","162067148","1323384","135470997","1272714","138932314","63043890","21070012","11748356","20980138","188888052","72082328","31101391","48545806","100929604" } local valueListC = {"169716851","169717184" } plys = game.Players.LocalPlayer function getMyPack() return game.Lighting:FindFirstChild(plys.Name .. "sPack") end local pack = getMyPack() --[[local messages = { ply.PlayerGui.Catalog.Frame1.TextLabel, ply.PlayerGui.Catalog.Frame1.hat, ply.PlayerGui.Catalog.Frame1.button} setMessageVisibility(messages, false)]] loadValues(valueList, pack.holder) loadValues(valueListF, pack.faces) loadValues(valueListC, pack.clothes) print("finished!!") --[[script.Parent.check.Value = 0 ply.PlayerGui.Catalog.Frame1.hat.Text = "Inventory items have been loaded!" local name = "Quad" ply.PlayerGui.Catalog.Frame1:TweenSizeAndPosition( UDim2.new(0.25, 0, 0.3, 0), UDim2.new(0.4, 0, 0.45, 0), "Out", name, 1, false) setMessageVisibility(messages, true) ]] end game.Players.PlayerAdded:connect(newPlayer)

Answer this question