Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

0

Is is in a local script? "Game.Players.LocalPlayer" is a better way to see what player has the local script.

0

I've been having the same problem with different types of scripts.

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

1 answer

  • 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

This discussion is preserved as it was. New answers, comments, and votes are closed.