Script not working how I want it to HELP

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local gui = script.Parent:WaitForChild("Gui")
local frame = gui.MainFrame

local infoFrame = frame:WaitForChild("InfoFrame")

local buildingFrame1 = frame:WaitForChild("BuildingsFrame1")
local cancelPlacementFrame = frame.CancelPlacement

local module = require(game.ReplicatedStorage.MainModule)
local eventsFolder = game.ReplicatedStorage:WaitForChild("Events")

local currentTab = 1

local function setUpText()
	local buildingData = (currentTab == 1) and module.barracks or module.uttility

	for i = 1, 4 do
		local button = buildingFrame1:FindFirstChild("Building" .. i)
		if button and button:IsA("ImageButton") then
			local buildingName = button:FindFirstChild("BuildingName")
			local building = buildingData["Building" .. i]

			if building then
				buildingName.Text = building.Name

				button.MouseEnter:Connect(function()
					buildingName.Text = "$" .. building.Price
				end)
				button.MouseLeave:Connect(function()
					buildingName.Text = building.Name
				end)
			else
				buildingName.Text = "No Building"
			end
		end
	end
end

local function switchTab(tabNumber)
	currentTab = tabNumber
	setUpText()
end

local function InFrame()
	buildingFrame1.Visible = true
	infoFrame.Visible = true
	local extendedPos = UDim2.new(0, 0, 0.874, 0)

	local tweenInfo = TweenInfo.new(0.7, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
	local tween = TweenService:Create(frame, tweenInfo, {Position = extendedPos})
	tween:Play()
end

local function OutFrame()
	buildingFrame1.Visible = false
	infoFrame.Visible = false
	local retractedPos = UDim2.new(0, 0, 0.956, 0)

	local tweenInfo = TweenInfo.new(0.7, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
	local tween = TweenService:Create(frame, tweenInfo, {Position = retractedPos})
	tween:Play()
end
OutFrame()

frame.MouseMoved:Connect(function()
	InFrame()
end)

frame.MouseLeave:Connect(function()
	wait(1)
	OutFrame()
end)

frame.Tab1Button.MouseButton1Click:Connect(function()
	switchTab(1)
	print(currentTab)
end)

frame.Tab2Button.MouseButton1Click:Connect(function()
	switchTab(2)
	print(currentTab)
end)

local hasChosen = false

local function placeBuilding(buildingData)
	hasChosen = true
	cancelPlacementFrame.Visible = true

	local buildingIndicatorModel = buildingData.model:Clone()

	local function cancelPlacement()
		hasChosen = false
		cancelPlacementFrame.Visible = false
		buildingIndicatorModel:Destroy()
	end

	local modelParts = buildingIndicatorModel:GetChildren()
	for i = 1, #modelParts do
		modelParts[i].Transparency = 0.5
		modelParts[i].CanCollide = false
		modelParts[i].CanQuery = false
		modelParts[i].Color = Color3.new(124, 124, 124)
	end

	buildingIndicatorModel.Parent = workspace

	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			if hasChosen and buildingIndicatorModel and buildingIndicatorModel.PrimaryPart then
				local rotationAngle = math.rad(45)
				local currentPosition = buildingIndicatorModel.PrimaryPart.CFrame
				local rotation = CFrame.Angles(0, rotationAngle, 0)
				local newPosition = currentPosition * rotation
				buildingIndicatorModel:SetPrimaryPartCFrame(newPosition)
			end
		end
	end)

	mouse.Button1Down:Once(function()
		if hasChosen and buildingIndicatorModel then
			local getChosenPosition = buildingIndicatorModel:GetPrimaryPartCFrame()
			eventsFolder.PlaceEvent:FireServer(mouse.Hit.p, buildingData, getChosenPosition)
			cancelPlacement()
		else
			warn("Error when placing!")
		end
	end)

	while hasChosen do
		buildingIndicatorModel:MoveTo(mouse.Hit.p)
		task.wait()

		cancelPlacementFrame.MouseEnter:Connect(cancelPlacement)
	end
end

for i = 1, 4 do
	local button = buildingFrame1:FindFirstChild("Building" .. i)
	local buildingData = (currentTab == 1) and module.barracks["Building" .. i] or module.uttility["Building" .. i]

	button.MouseButton1Click:Connect(function()
		if not hasChosen then
			placeBuilding(buildingData)
		end
	end)
end

Sorry for long script, this is a building system (local), you can change between tabs 1/2. When you are in tab 1 you should be able to work whit the barracks, on tab 2 whit uttility, even tho I added this

local buildingData = (currentTab == 1) and module.barracks or module.uttility

To make sure it will do just that, when I change beetwen tabs it places just the barracks type and for the hovering over buttons it will always show the barrack name instead of the uttility.

Example

uttility = {
		["Building1"] = {
			Name = "Hospital",
			Price = 50,
			model = buildingsFolder.Uttility.Hospital
		};

when i am on tab 2 and i hover over a button it should use these untill you change tabs, but it only works once, meaning that if you hover to show the price, after stopping to hover, it will show barrack name.

Also it wont place the good model, meaning always barrack model.

Tell me if you need clarifications

1 Like