Bug with UI text labels

Im having Issues with my UI not detecting input from a TextButton and also the display text on a text lable not updating when the Text value is changed. Ill give the script and the download file for the game

I dont need this to be completed, just the button and text to work. But for context to help solve the problem im trying to make a click-to-equip so that I can test viewmodels. Every frame in the list will use the module to display the weapon icon and name.

main script (This is called Framework - Client under StarterPlayerScripts)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local aimCF = CFrame.new()

local mouse = player:GetMouse()

local isAiming = false
local isShooting = false
local isReloading = false
local canShoot = true

local debounce = false

local currentSwayAMT = 0
local swayAMT = 0.6
local aimSwayAMT = -0.2
local swayCF = CFrame.new()
local lastCameraCF = CFrame.new()

local currentSlot = 1 -- starting slot
local lastSlot = 0
local maxSlots = 3

local fireAnim = nil

local particle
local flash
local bigFlash
local timer
local bobOffset = CFrame.new()

local framework = {
	inventory = {
		"Rocket Launcher";
		"Paintball Gun";
		"Sword";
	};
	
	module = nil;
	viewmodel = nil;
	currentSlot = 1;
}

local hud = player.PlayerGui:WaitForChild("GameUI")

function deleteViewmodel()
	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
end

function setInventory()
	framework.inventory[1] = "Paintball Gun"
	framework.inventory[2] = "Rocket Launcher"
	framework.inventory[3] = "Sword"
end

function loadSlot(Item)
	local viewmodelFolder = game.ReplicatedStorage.ViewModels
	local moduleFolder = game.ReplicatedStorage:WaitForChild("Modules")
	
	isShooting = false

	deleteViewmodel()

	if moduleFolder then
		framework.module = require(moduleFolder:WaitForChild(Item))
		if viewmodelFolder:FindFirstChild(Item) then
			framework.viewmodel = viewmodelFolder:FindFirstChild(Item):Clone()
			framework.viewmodel.Parent = camera
		end
		print(framework.module.ammo)
		if framework.viewmodel and framework.module and character then
			-- animation
			fireAnim = Instance.new("Animation")
			fireAnim.Parent = framework.viewmodel
			fireAnim.Name = "Fire"
			fireAnim.AnimationId = framework.module.fireAnim
			fireAnim = framework.viewmodel.AnimationController.Animator:LoadAnimation(fireAnim)
			-- sound
			game.ReplicatedStorage.Events.LoadSlot:FireServer(framework.module.fireSound)
			flash = framework.viewmodel.HumanoidRootPart.Attachment.flash
			bigFlash = framework.viewmodel.HumanoidRootPart.Attachment.bigFlash
			hud.gun.Icon.Image = framework.module.icon
		end
	end
end

local function refreshList()
	local listOfWepons = game:GetService("StarterGui").GunSelector.List.List:GetChildren()
	local text = nil
	local image = nil

	for i, v in pairs(listOfWepons) do
		if v:IsA("Frame") then
			if v.Name ~= "Empty" then
				text = v:WaitForChild("Icon"):WaitForChild("id").Text
				--v.Icon.id.Text = v.Name
				v.Icon.id.Text = v.Name
				print(text)
				print(v.Icon.id.Text)
			end
		end
	end
end

game:GetService("StarterGui"):WaitForChild("GunSelector"):WaitForChild("List"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
	print("click")
	refreshList()
end)

RunService.RenderStepped:Connect(function()
	local rot = camera.CFrame:ToObjectSpace(lastCameraCF)
	local X, Y, Z = rot:ToOrientation()
	swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * currentSwayAMT, math.sin(Y) * currentSwayAMT, 0), .1)
	lastCameraCF = camera.CFrame

	if framework.viewmodel and framework.module then
		hud.gun.Icon.id.Text = framework.inventory[framework.currentSlot]
		hud.ammo.amount.Text = framework.module.ammo .. "/" .. framework.module.maxAmmo
	end

	if humanoid then
		local movementOffset = CFrame.new()

		if framework.viewmodel ~= nil and framework.module ~= nil then
			if humanoid.MoveDirection.Magnitude > 0 and humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
				if isReloading == true then
					bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.Y/1, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), .005)
				else
					if isAiming == true then
						bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.Y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), .005)
					else
						bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.Y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), humanoid.WalkSpeed/45)
					end
				end
			end
		else
			bobOffset = bobOffset:Lerp(CFrame.new(0, -humanoid.CameraOffset.Y/3, 0), .1)
		end

	end

	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v.PrimaryPart.CFrame = (camera.CFrame * swayCF * aimCF * bobOffset)
			--updateCameraShake()
		end
	end

	if isAiming and framework.viewmodel ~= nil and framework.module.canAim then
		local offset = framework.viewmodel.AimCamera.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
		aimCF = aimCF:Lerp(offset, framework.module.aimSmooth)
		currentSwayAMT = aimSwayAMT
	else
		local offset = CFrame.new()
		aimCF = aimCF:Lerp(offset, .2)
		currentSwayAMT = swayAMT
	end
	
	-- HUD
	if hud and moduleFolder == moduleFolder then
		if framework.module.ammo < framework.module.maxAmmo/4 then
			hud.ammo.Icon.ImageColor3 = Color3.fromRGB(255, 0, 0) 
		else
			hud.ammo.Icon.ImageColor3 = Color3.new(1, 1, 1)
		end

		if isReloading == true then
			hud.ammo.Icon.reload.Visible = true
			hud.ammo.Icon.ImageTransparency = 0.75
		else
			hud.ammo.Icon.reload.Visible = false
			hud.ammo.Icon.ImageTransparency = 0
		end
	end
end)

local function updateWeapon(slot)
	if isReloading == true and canShoot == false and framework.module.ammo ~= 0 then
		isReloading = false
		canShoot = true
	end
	if isReloading == false then
		framework.currentSlot = slot
		loadSlot(framework.inventory[currentSlot])
	end
	refreshList()
end

updateWeapon(1)


-- SLOT CHANGING
local function changeSlot(howMuch)
	currentSlot += howMuch
	if currentSlot > maxSlots then
		currentSlot = 1
	elseif currentSlot < 1 then
		currentSlot = maxSlots
	end
	updateWeapon(currentSlot)
end

local function setSlot(whatSlot)
	if framework.currentSlot ~= whatSlot then
		currentSlot = whatSlot
	end
	updateWeapon(currentSlot)
end

local function reload()
	if isReloading == false and framework.module.maxAmmo ~= 0 and framework.module.ammo ~= framework.module.maxAmmo then
		canShoot = false
		isReloading = true
		
		wait(framework.module.reloadTime)
		
		if isReloading == true then
			framework.module.ammo = framework.module.maxAmmo
		end
		canShoot = true
		isReloading = false
		
	end
end

-- SHOOTING --
local function fireWeapon()
	if character and framework.viewmodel and framework.module and framework.module.ammo ~= 0 and debounce == false and isReloading == false	and canShoot == true and framework.module.maxAmmo ~= 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		character.Torso.FireSound:Play()
		framework.module.ammo -= 1
		debounce = true

		wait(framework.module.fireRate)

		debounce = false
	elseif character and framework.viewmodel and framework.module and debounce == false and framework.module.maxAmmo == 0 then
		game.ReplicatedStorage.Events.Shoot:FireServer()
		fireAnim:Play()
		character.Torso.FireSound:Play()
		debounce = true

		wait(framework.module.fireRate)

		debounce = false
	end
end

local function trigger()
	if isReloading == true and canShoot == false and framework.module.ammo ~= 0 then
		isReloading = false
		canShoot = true
	end
	
	if framework.module.fireMode == "Semi" then
		fireWeapon()
	end

	if framework.module.fireMode == "Full Auto" then
		isShooting = true
	end
end

local oldCamCF = CFrame.new()

function updateCameraShake()
	local newCamCF = framework.viewmodel.FakeCamera.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
	camera.CFrame = camera.CFrame * newCamCF:ToObjectSpace(oldCamCF)
	oldCamCF = newCamCF
end


-- INPUTS -- 

--UserInputService.MouseIconEnabled = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		setSlot(1)
	end
	
	if input.KeyCode == Enum.KeyCode.Two then
		setSlot(2)
	end
	
	if input.KeyCode == Enum.KeyCode.Three then
		setSlot(3)
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL1 then
			changeSlot(1)
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR1 then
			changeSlot(-1)
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
		print("middleButton")
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = true
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if character and framework.viewmodel and framework.module then
			trigger()
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL2 then
			isAiming = true
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR2 then
			trigger()
		end
	end
	
	-- RELOAD
	if input.KeyCode == Enum.KeyCode.R then
		reload()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = false
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL2 then
			isAiming = false
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isShooting = false
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR2 then
			isShooting = false
		end
	end
end)

mouse.WheelForward:Connect(function()
	changeSlot(1)
end)

mouse.WheelBackward:Connect(function()
	changeSlot(-1)
end)

game.ReplicatedStorage.Events.PlayerAdded.OnClientEvent:Connect(function(ply, char)
	
	player = game.Players.LocalPlayer
	
	character = player.Character
	
	humanoid = character:WaitForChild("Humanoid")
	
	setInventory()
	
	framework.module.ammo = framework.module.maxAmmo

	framework.module = nil
	framework.viewmodel = nil
	framework.currentSlot = 1
	

	loadSlot(framework.inventory[currentSlot])

	humanoid.Died:Connect(function()
		
		deleteViewmodel()
		
		local isAiming = false
		local isShooting = false
		local isReloading = false
		local canShoot = true

		local debounce = false

		local currentSwayAMT = 0
		local swayAMT = 0.6
		local aimSwayAMT = -0.2
		local swayCF = CFrame.new()
		local lastCameraCF = CFrame.new()

		local currentSlot = 1 -- starting slot
		local lastSlot = 0
		local maxSlots = 3

		local fireAnim = nil

		local particle
		local flash
		local bigFlash
		local timer
		local bobOffset = CFrame.new()
	end)
end)  


while wait() do
	if isShooting and framework.module.ammo > 0 and isReloading ~= true and canShoot == true then
		framework.module.ammo -= 1
		fireAnim:Play()
		game.ReplicatedStorage.Events.Shoot:FireServer()
		mouse.Button1Up:Connect(function()
			isShooting = false
		end)
		
		wait(framework.module.fireRate)
	end
end

and heres the download file:
Framework Testing Untitled Games Development.rbxl (132.3 KB)

If anyone has any suggestions or solutions to fixing this please let me know!

Whenever your connecting the button to a click function, your using starter gui which isn’t where the players ui is located. Starter gui gets replicated to each player in something called the playergui located inside the player instance so since this is a local script and you have a reference to the player, just do:

player.PlayerGui:WaitForChild("GunSelector"):WaitForChild("List"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
	print("click")
	refreshList()
end)

Also, wait() is deprecated so you should be using task.wait()

2 Likes

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