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

Class GUI works in studio but not when played on ROBLOX player?

Asked by 6 years ago
Edited 6 years ago

This script seems to be working on studio but does not work when I play the game through ROBLOX player, is there anything that needs to be changed? Thanks! (This is in a Local Script)

player = script.Parent.Parent.Parent
backpack = player.Backpack

function chooseClass(class)
    for i, v in pairs(backpack:GetChildren()) do v:remove() end
    for i, v in pairs(class:GetChildren()) do
        if v:IsA("Tool") then
            v:clone().Parent = backpack
        elseif v:IsA("HopperBin") then
            v:clone().Parent = backpack
        end

    end


    script.Parent.Frame.Visible = false 
    script.Parent.Cover.Visible = false
    script.Parent.Title.Visible = false
end

function onHumanoidDied(humanoid, player)
    script.Parent.Frame.Visible = true
    script.Parent.Cover.Visible = true
    script.Parent.Title.Visible = true
    end

for i, v in pairs(script.Parent.Cover:GetChildren()) do
    v.MouseButton1Up:connect(function () chooseClass(v) end)
end

0
Is is in a local script? "Game.Players.LocalPlayer" is a better way to see what player has the local script. Bellyrium 310 — 6y
0
I've been having the same problem with different types of scripts. Prideful_Ryan 38 — 6y
1
Well, the lag would be a cause. This script doesnt wait for the children.. Things like that and if the script is local, if you have changed studio settings, and a few other things can effect it.. Knowing if it gives an error helps. Also learning to use the LuaDebugger Bellyrium 310 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago
  • An easier way to get the client from a localscript is using:

    game.Players.LocalPlayer -


  • Secondly, you should use the WaitForChild function to reference the Player's Backpack. The Backpack may have not loaded yet, which would return an error like:

    Attempt to index a nil value 'Backpack'

  • Another thing.. on line 9 you have an elseif statement that repeats the same code as above.. you can turn this into a compound if statement, using the or operator! I'll show you below.

  • And finally, remove is deprecated. Use Destroy!
local player = game.Players.LocalPlayer; --Use LocalPlayer
local backpack = player:WaitForChild("Backpack"); --Use WaitForChild

function chooseClass(class)
    for i, v in pairs(backpack:GetChildren()) do 
        v:Destroy() --Use Destroy
    end
    for i, v in pairs(class:GetChildren()) do
        if v:IsA("Tool") or v:IsA("HopperBin") then --Use 'or'
            v:Clone().Parent = backpack
        end
    end
end

function set(b) --For convenience! You had repeating code again
    script.Parent.Frame.Visible = b
    script.Parent.Cover.Visible = b
    script.Parent.Title.Visible = b
end

set(false);

function onHumanoidDied(humanoid, player)
    set(true);
end

for i, v in pairs(script.Parent.Cover:GetChildren()) do
    v.MouseButton1Up:connect(function() 
        chooseClass(v) 
    end)
end
Ad

Answer this question