SurfaceGUI Problem

@jackWorhee i think because those things in the screenshot are added some time after this LocalScript is run, so it doesn’t pick up the NumberBack at that moment

This is the code.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClothesFolder = ReplicatedStorage:WaitForChild("Clothes"):WaitForChild("Player")

local takenNumbers = {}

local function getAvailableNumber()
	local availableNumbers = {}
	for i = 1, 456 do
		local numStr = string.format("%03d", i)
		if not takenNumbers[numStr] then
			table.insert(availableNumbers, numStr)
		end
	end
	if #availableNumbers > 0 then
		local randomIndex = math.random(1, #availableNumbers)
		local assignedNumber = availableNumbers[randomIndex]
		takenNumbers[assignedNumber] = true
		return assignedNumber
	end
	return "000"
end

local function Blocky(character)
	local humanoid = character:WaitForChild("Humanoid")
	wait()
	local currentDescription = humanoid:GetAppliedDescription()
	currentDescription.Head = 0
	currentDescription.Torso = 0
	currentDescription.LeftArm = 0
	currentDescription.RightArm = 0
	currentDescription.LeftLeg = 0
	currentDescription.RightLeg = 0
	humanoid:ApplyDescription(currentDescription)
end

local function onCharacterAdded(character)
	Blocky(character)

	for _, obj in ipairs(character:GetChildren()) do
		if obj:IsA("Shirt") or obj:IsA("Pants") then
			obj:Destroy()
		elseif obj:IsA("Accessory") then
			if obj.AccessoryType ~= Enum.AccessoryType.Hat and obj.AccessoryType ~= Enum.AccessoryType.Face and obj.AccessoryType ~= Enum.AccessoryType.Hair then
				obj:Destroy()
			end
		end
	end

	local newShirt = ClothesFolder:FindFirstChild("Shirt")
	local newPants = ClothesFolder:FindFirstChild("Pants")
	local newGUI = ClothesFolder:FindFirstChild("NumberFront")
	local backGUI = ClothesFolder:FindFirstChild("NumberBack")

	if newShirt then
		local shirtClone = newShirt:Clone()
		shirtClone.Parent = character
	end

	if newPants then
		local pantsClone = newPants:Clone()
		pantsClone.Parent = character
	end

	if newGUI then
		local upperTorso = character:FindFirstChild("UpperTorso")
		if upperTorso then
			local guiClone = newGUI:Clone()
			guiClone.Parent = upperTorso
			local label = guiClone:FindFirstChild("NumberText")
			if label and label:IsA("TextLabel") then
				if not character.Parent:GetAttribute("AssignedNumber") then
					local assignedNumber = getAvailableNumber()
					character.Parent:SetAttribute("AssignedNumber", assignedNumber)
				end
				label.Text = character.Parent:GetAttribute("AssignedNumber")
			end
		end
	end
	
	if backGUI then
		local upperTorso = character:FindFirstChild("UpperTorso")
		if upperTorso then
			local guiClone = backGUI:Clone()
			guiClone.Parent = upperTorso
			local label = guiClone:FindFirstChild("NumberText")
			if label and label:IsA("TextLabel") then
				if not character.Parent:GetAttribute("AssignedNumber") then
					local assignedNumber = getAvailableNumber()
					character.Parent:SetAttribute("AssignedNumber", assignedNumber)
				end
				label.Text = character.Parent:GetAttribute("AssignedNumber")
			end
		end
	end
	
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end

local function onPlayerRemoving(player)
	local assignedNumber = player:GetAttribute("AssignedNumber")
	if assignedNumber then
		takenNumbers[assignedNumber] = nil
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

This code is a Server Script, And runs in ServerScriptService.

Yes but then why didn’t the script with the loop work? It should also stop yielding after some time then which it doesn’t.

To OP @yHaruRBLX. Did this script

local Game,Workspace,Script = game,workspace,script
local RunService = Game:GetService("RunService")
local Camera = Workspace.CurrentCamera

local Character = Script.Parent
local Head = Character:WaitForChild("Head")
local UpperTorso = Character:WaitForChild("UpperTorso")
repeat task.wait() print(UpperTorso:FindFirstChild("NumberFront"),UpperTorso:FindFirstChild("NumberBack")) until UpperTorso:FindFirstChild("NumberBack")
local Gui = UpperTorso.NumberBack

local function ToggleState()
	if (Camera.CFrame.Position - Head.Position).Magnitude < 1 then
		Gui.Enabled = false
	else
		Gui.Enabled = true
	end
end

RunService.RenderStepped:Connect(ToggleState)

Work after some time or not?

I also added a print statement and I want to know what it prints. It should print NumberFront NumberBack.

This script doesn’t work, Doesn’t print anything

just another thought, if we merely want to β€˜turn off’ my own number gui,
maybe it is simpler to put the LocalScript inside the NumberBack instead,
and it does this:

while not script.Parent do task.wait() end
local Gui = script.Parent -- NumberBack
while not Gui.Parent do task.wait() end -- not sure this is needed
local UpperTorso = Gui.Parent
while not UpperTorso.Parent do task.wait() end -- not sure this is needed
local Character = UpperTorso.Parent

local LocalPlayer = game:GetService("Players").LocalPlayer
if LocalPlayer.Character == Character then
  Gui.Enabled = false
end

I made a new local script, put it inside the NumberBack.

First, From the Other Local Script:

16:03:19.187 NumberFront NumberBack - Client - TEST:8
16:03:34.955 NumberFront NumberBack - Client - TEST:8

It is actually doing something, If both scripts are enabled, Im running in first person, its Flashing rapidly. However if i disable the TEST script (1st local script) then it will not do anything.

I made few adjustements and this code WORKS! Thank you guys all for the help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.