Hello I am having trouble my unit for my gui and i dont know why the script doesnt have any errors can anyine help
Heres the script
local function showUnitInfo()
print("showUnitInfo triggered")
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
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)
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 = game.StarterGui:WaitForChild("MainGui")
local invalidPlacementMessage = inventoryandhotbarsui:WaitForChild("CantPlaceLabel")
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 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
local function removePlacedUnits()
if unitCanSpawn then
unitCanSpawn:Destroy()
unitCanSpawn = nil
rotation = 0
end
end
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
local function colorUnitHolder(color)
for i, object in pairs(unitCanSpawn:GetDescendants()) do
if object:IsA("BasePart") then
object.Color = color
end
end
end
inventoryandhotbarsui.Units.Text = "Units: " .. placedUnits .. "/" .. maxUnits
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
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
local function showUnitInfo()
print("showUnitInfo triggered")
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
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)
when i click on the unit it doesnt show the gui
It looks like you’re editing the gui in game.StarterGui, but you should instead be editing the gui inside player.PlayerGui. When a player joins the game, all the guis inside game.StarterGui get :Clone()'d and placed into the player.PlayerGui. So players each have their own copy of the template from game.StarterGui.
I mean you still haven’t described what the actual problem you’re experiencing is, but I’m just taking a guess here at what might be wrong by reading the code.
Try changing this to local mainGui = players.LocalPlayer.PlayerGui:WaitForChild("MainGui")