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
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'
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.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