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 7 years ago
Edited 7 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)

01player = script.Parent.Parent.Parent
02backpack = player.Backpack
03 
04function chooseClass(class)
05    for i, v in pairs(backpack:GetChildren()) do v:remove() end
06    for i, v in pairs(class:GetChildren()) do
07        if v:IsA("Tool") then
08            v:clone().Parent = backpack
09        elseif v:IsA("HopperBin") then
10            v:clone().Parent = backpack
11        end
12 
13    end
14 
15 
View all 29 lines...
0
Is is in a local script? "Game.Players.LocalPlayer" is a better way to see what player has the local script. Bellyrium 310 — 7y
0
I've been having the same problem with different types of scripts. Prideful_Ryan 38 — 7y
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 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 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!
01local player = game.Players.LocalPlayer; --Use LocalPlayer
02local backpack = player:WaitForChild("Backpack"); --Use WaitForChild
03 
04function chooseClass(class)
05    for i, v in pairs(backpack:GetChildren()) do
06        v:Destroy() --Use Destroy
07    end
08    for i, v in pairs(class:GetChildren()) do
09        if v:IsA("Tool") or v:IsA("HopperBin") then --Use 'or'
10            v:Clone().Parent = backpack
11        end
12    end
13end
14 
15function set(b) --For convenience! You had repeating code again
View all 31 lines...
Ad

Answer this question