Gui not showing the right stats

Hey so I have this script that I want to display the units stats on. When the mouse hovers over the unit button the it should show the units stats on a gui but it doesnt show the stats on the gui and I dont know why. The stats that should be shown should be the Cooldown, Damage, and Range but it doesnt show it.Can anyone help?
Heres the script

local player = players.LocalPlayer
local hotbarsFrames = player.PlayerGui:WaitForChild("Inventory/Hotbars"):WaitForChild("Hotbars")
local display = mainGui:WaitForChild("DisplayGui")

display.Visible = false

local function positionDisplayFrame(button)
	local buttonPosition = button.AbsolutePosition
	local buttonSize = button.AbsoluteSize
	display.Position = UDim2.new(0, buttonPosition.X, 0.403, 0, buttonPosition.Y + buttonSize.Y) 
end

local function updateDisplayInfo()
    if selectedUnit then
	local configuration = selectedUnit.Configuration
		display.Stats.Cooldown.Value.Text = configuration.Cooldown.Value
		display.Stats.Damage.Value.Text = configuration.Damage.Value
		display.Stats.Range.Value.Text = configuration.Range.Value
		display.UnitName.Text = selectedUnit.Name
	end
end

local function connectButtonEvents(button)
	button.MouseEnter:Connect(function()
		positionDisplayFrame(button)
		updateDisplayInfo()
		display.Visible = true
	end)

	button.MouseLeave:Connect(function()
		display.Visible = false
	end)
end

for _, button in pairs(hotbarsFrames:GetChildren()) do
	if button:IsA("ImageButton") then
		connectButtonEvents(button)
	end
end

hotbarsFrames.ChildAdded:Connect(function(child)
	if child:IsA("ImageButton") then
		connectButtonEvents(child)
	end
end)
4 Likes

You have 5 parameters here, should be UDim2.new(xScale, xOffset, yScale, yOffset)


I don’t see selectedUnit anywhere else in your code, you could fix this by adding it as the function’s parameter:

-- Change to:
local function updateDisplayInfo(selectedUnit)

-- And under connectButtonEvents function:
updateDisplayInfo(button)
3 Likes

Ok so i did that and it showed this error in output

 Configuration is not a valid member of ImageButton "Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Hotbars.Customer"  -  Client - Controlla:76
  00:29:52.089  Stack Begin  -  Studio
  00:29:52.089  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 76 - function updateDisplayInfo  -  Studio - Controlla:76
  00:29:52.090  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 87  -  Studio - Controlla:87
  00:29:52.090  Stack End
3 Likes

I can also provide the full script if thats easier

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")

local moudlesFolder = replicatedStorage:WaitForChild("ModuleScripts")
local health = require(moudlesFolder:WaitForChild("Health"))

local eventsFolder = replicatedStorage:WaitForChild("Events")
local functionsFolder = replicatedStorage:WaitForChild("Functions")
local requestFunction = functionsFolder:WaitForChild("RequestingUnit")
local unitsFolder = replicatedStorage:WaitForChild("Units")
local placeUnitEvent = eventsFolder:WaitForChild("PlaceUnitEvent")

local inventoryandhotbarsui = script.Parent
local mainGui = players.LocalPlayer.PlayerGui:WaitForChild("MainGui")
local invalidPlacementMessage = inventoryandhotbarsui:WaitForChild("CantPlaceLabel")
local displayGui = mainGui:WaitForChild("DisplayGui")

local coins = players.LocalPlayer:WaitForChild("Coins")
local map = workspace.Maps:WaitForChild("FoodMap")
local resaurantBase = map:WaitForChild("Restaurant")
local info = workspace:WaitForChild("Info")

local hoveredValue = nil
local selectedUnit = nil
local camera = workspace.CurrentCamera
local unitCanSpawn = nil
local canPlace = false
local rotation = 0
local placedUnits = 0
local maxUnits = 16

local function setUpGui()
	health.Setup(resaurantBase, inventoryandhotbarsui.Info.Health)

	workspace.FoodEnemys.ChildAdded:Connect(function(foodEnemy)
		health.Setup(foodEnemy)
	end)

	info.Message.Changed:Connect(function(change)
		invalidPlacementMessage.Parent.Info.Message.Text = change
		if change == "" then
			inventoryandhotbarsui.Info.Message.Visible = false
		else
			inventoryandhotbarsui.Info.Message.Visible = true
		end
	end)

	info.Wave.Changed:Connect(function(change)
		inventoryandhotbarsui.Info.Stats.Wave.Text = "Wave: " .. change
	end)

	coins.Changed:Connect(function(change)
		inventoryandhotbarsui.Info.Stats.Coins.Text = "$" .. coins.Value
	end)
	inventoryandhotbarsui.Info.Stats.Coins.Text = "$" .. coins.Value
end
setUpGui()

local player = players.LocalPlayer
local hotbarsFrames = player.PlayerGui:WaitForChild("Inventory/Hotbars"):WaitForChild("Hotbars")
local display = mainGui:WaitForChild("DisplayGui")

display.Visible = false

local function positionDisplayFrame(button)
	local buttonPosition = button.AbsolutePosition
	local buttonSize = button.AbsoluteSize
	display.Position = UDim2.new(0, buttonPosition.X, 0.403, 0, buttonPosition.Y + buttonSize.Y) 
end

local function updateDisplayInfo(selectedUnit)
    if selectedUnit then
	local configuration = selectedUnit.Configuration
		display.Stats.Cooldown.Value.Text = configuration.Cooldown.Value
		display.Stats.Damage.Value.Text = configuration.Damage.Value
		display.Stats.Range.Value.Text = configuration.Range.Value
		display.UnitName.Text = selectedUnit.Name
	end
end

local function connectButtonEvents(button)
	button.MouseEnter:Connect(function()
		positionDisplayFrame(button)
		updateDisplayInfo(button)
		display.Visible = true
	end)

	button.MouseLeave:Connect(function()
		display.Visible = false
	end)
end

for _, button in pairs(hotbarsFrames:GetChildren()) do
	if button:IsA("ImageButton") then
		connectButtonEvents(button)
	end
end

hotbarsFrames.ChildAdded:Connect(function(child)
	if child:IsA("ImageButton") then
		connectButtonEvents(child)
	end
end)

-- Function to perform raycast
local function mouseRayCast(blacklist)
	local whereMouse = userInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(whereMouse.X, whereMouse.Y)
	local rayCastParams = RaycastParams.new()

	rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
	rayCastParams.FilterDescendantsInstances = blacklist

	local rayCastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, rayCastParams)
	return rayCastResult
end

-- Function to remove placed units
local function removePlacedUnits()
	if unitCanSpawn then
		unitCanSpawn:Destroy()
		unitCanSpawn = nil
		rotation = 0
	end
end

-- Function to add a unit to the workspace
local function addUnit(name)
	local unitExists = unitsFolder:FindFirstChild(name)
	removePlacedUnits()
	if unitExists then
		unitCanSpawn = unitExists:Clone()
		unitCanSpawn.Parent = workspace

		for i, object in pairs(unitCanSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Units"
				object.Material = Enum.Material.ForceField
			end
		end
	end
end

-- Function to color the unit holder
local function colorUnitHolder(color)
	for i, object in pairs(unitCanSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color
		end
	end
end

-- Function to fade out the invalid placement message
local function fadeOutMessage()
	local duration = 2 
	local startTransparency = 0 
	local endTransparency = 1 

	for i = 0, 1, 1/(duration*10) do
		invalidPlacementMessage.TextTransparency = startTransparency + (endTransparency - startTransparency) * i
		wait(1/10) 
	end

	invalidPlacementMessage.Visible = false 
end

-- Setup buttons in hotbars
for i, unit in pairs(unitsFolder:GetChildren()) do
	if unit:IsA("Model") then
		local button = inventoryandhotbarsui.Hotbars.Temp:Clone()
		local configuration = unit:WaitForChild("Configuration")
		button.Name = unit.Name
		button.Image = configuration.Image.Texture
		button.Visible = true
		button.LayoutOrder = configuration.Price.Value
		button.Price.Text = configuration.Price.Value

		button.Parent = inventoryandhotbarsui.Hotbars

		button.Activated:Connect(function()
			local allowingSpawn = requestFunction:InvokeServer(unit.Name)
			if placedUnits < maxUnits then
				addUnit(unit.Name)
			end
		end)
	end
end

-- Function to show unit info in GUI
local function showUnitInfo()
	if selectedUnit then
		mainGui.Selection.Visible = true
		local configuration = selectedUnit.Configuration
		mainGui.Selection.Stats.Damage.Value.Text = configuration.Damage.Value
		mainGui.Selection.Stats.Range.Value.Text = configuration.Range.Value
		mainGui.Selection.Stats.Cooldown.Value.Text = configuration.Cooldown.Value
		mainGui.Selection.Title.UnitName.Text = selectedUnit.Name
		mainGui.Selection.Title.ImageLabel.Image = configuration.Image.Texture
	end
end

-- Handle user input
userInputService.InputBegan:Connect(function(input, processed)
	if processed then 
		return 
	end

	if unitCanSpawn then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if canPlace then
				placeUnitEvent:FireServer(unitCanSpawn.Name, unitCanSpawn.PrimaryPart.CFrame)
				placedUnits += 1
				inventoryandhotbarsui.Units.Text = "Units: " .. placedUnits .. "/" .. maxUnits
				removePlacedUnits()
				invalidPlacementMessage.Visible = false 
			else
				invalidPlacementMessage.Visible = true 
				invalidPlacementMessage.TextTransparency = 0 
				fadeOutMessage() 
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			rotation += 90
		end
	elseif hoveredValue and input.UserInputType == Enum.UserInputType.MouseButton1 then
		local model = hoveredValue:FindFirstAncestorOfClass("Model")

		if model and model.Parent == workspace.Units then
			selectedUnit = model
		else
			selectedUnit = nil
		end

		showUnitInfo()
	end
end)

-- Update the unit placement and rotation in real-time
runService.RenderStepped:Connect(function()
	local result = mouseRayCast({unitCanSpawn})
	if result and result.Instance then
		if unitCanSpawn then
			hoveredValue = nil

			if result.Instance.Parent.Name == "UnitsArea" then
				canPlace = true
				colorUnitHolder(Color3.new(0, 1, 0))
			else
				canPlace = false
				colorUnitHolder(Color3.new(1, 0, 0))
			end

			local x = result.Position.X
			local y = result.Position.Y + unitCanSpawn.Humanoid.HipHeight + (unitCanSpawn.PrimaryPart.Size.Y / 0.8)
			local z = result.Position.Z
			local cframe = CFrame.new(x, y, z) * CFrame.Angles(0, math.rad(rotation), 0)
			unitCanSpawn:SetPrimaryPartCFrame(cframe)
		else
			hoveredValue = result.Instance
		end
	else
		hoveredValue = nil 
	end
end)
4 Likes

Ah I assumed Configuration was under the buttons. Undo the change to the updateDisplayInfo(selectedUnit) and updateDisplayInfo(button)

Also can you elaborate on what’s “not showing”, like is the gui completely missing, or are the text-stats just not updating?

3 Likes

This UI on the screen isnt showing the right values

4 Likes

Okay I’ve scanned through the script, sorry for the complicated reply not sure how to condense it.

It looks like selectedUnit is the model your mouse is hovering over. But we’re working with UI buttons here, hence why theres no change when you mouse-enter the button. So you’ve got two options:

  1. When hovering over a button, if you can figure out the corresponding model for that button you can grab that model’s configuration stats.
local function updateDisplayInfo(button)
	-- EXAMPLE
	local model = WHERE_YOUR_MODELS_ARE:FindFirstChild(button.ModelName.Value)
	if model then
		local configuration = model.Configuration
		-- ... rest of function
  1. You can manually copy the configurations into your buttons instead, but this assumes the config of your models don’t change.
local function updateDisplayInfo(button)
	local configuration = button.Configuration
	-- ... rest of function

I’m afraid I have to make a lot of assumptions about your game’s structure so I hope this helps out!

4 Likes

It just says this

 ModelName is not a valid member of ImageButton "Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Hotbars.BurgerMan"  -  Client - Controlla:74
  01:11:33.298  Stack Begin  -  Studio
  01:11:33.298  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 74 - function updateDisplayInfo  -  Studio - Controlla:74
  01:11:33.298  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 87  -  Studio - Controlla:87
  01:11:33.299  Stack End
2 Likes

You have to add a StringValue to the button that would point you to the correct model by name. You could also use an ObjectValue to point you to the model directly if you prefer

2 Likes

I did that and it shows this

 argument #1 expects a string, but Instance was passed  -  Client - Controlla:74
  01:18:57.569  Stack Begin  -  Studio
  01:18:57.569  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 74 - function updateDisplayInfo  -  Studio - Controlla:74
  01:18:57.569  Script 'Players.Toastystrudel27.PlayerGui.Inventory/Hotbars.Controlla', Line 87  -  Studio - Controlla:87
  01:18:57.569  Stack End
2 Likes

If you did an ObjectValue instead of StringValue

-- change this
local model = WHERE_YOUR_MODELS_ARE:FindFirstChild(button.ModelName.Value)
-- into this:
local model = button.ModelName.Value
2 Likes

Nope still didnt work

local function updateDisplayInfo(button)
	local unit = button.ModelName.Value
	if unit then
	local configuration = unit.Configuration
	if selectedUnit then
		display.Stats.Cooldown.Value.Text = configuration.Cooldown.Value
		display.Stats.Damage.Value.Text = configuration.Damage.Value
		display.Stats.Range.Value.Text = configuration.Range.Value
		display.UnitName.Text = selectedUnit.Name
		end
	end
end

local function connectButtonEvents(button)
	button.MouseEnter:Connect(function()
		positionDisplayFrame(button)
		updateDisplayInfo(button)
		display.Visible = true
	end)

	button.MouseLeave:Connect(function()
		display.Visible = false
	end)
end
2 Likes

remove if selectedUnit then we don’t need it here (thats the model your mouse is hovering)
and change display.UnitName.Text = selectedUnit.Name → display.UnitName.Text = unit.Name

3 Likes

Cool it works thank you very much

3 Likes

Hi there, mark the above post as the solution if it works to close the topic!

3 Likes

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