House shop script not working

I am working on a game I have given up on a year ago. I am trying to make a shop where you can buy houses, then equip a house you want to spawn after. A year ago I scripted it all myself from scratch but the script was messy, bad and it would take too long to add more houses to the shop. Now I used ChatGPT to make the script better. Problem is: I don’t get any error messages about the script when I playtest, but when I click on the buy button nothing happens. What should happen is it should reduce the money from the player and the buy button Visible should be changed to false and the equip button should show. The script is running. Path to the button is game.StarterGUI.Shop2.CloseFrame.BuildingsFrame.House1[“BUY 1”]

TLDR: Script for a shop GUI for buying houses doesn’t work, when I press buy nothing happens.

Here is the script:

local player = game.Players.LocalPlayer
local equippedHouseFolder = game.ReplicatedStorage.equippedHouse -- Reference to the folder where equipped houses will be stored
print("HousePurchase script running")

	-- Function to handle house purchase
local function purchaseHouse(houseName, housePrice, purchaseButton, equipButton)
		-- Check if the player has enough money to buy the house
		if player.leaderstats.Cash.Value >= housePrice then
		-- Deduct the house price from the player's money
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - housePrice

		-- Disable the "Purchase" button for the purchased house
		purchaseButton.Visible = false

		-- Enable the "Equip" button for the purchased house
		equipButton.Visible = true
	else
		print("not enough money, stranger!")
		-- REMINDER TO MYSELF: MAKE THE BUTTON RED FOR A SECOND OR SUMTHIN IF PLAYER IS TOO BROKE
	end
	end

-- Function to handle house equipping
local function equipHouse(houseName)
	-- Remove any previously equipped house from the equippedHouseFolder
	for _, child in ipairs(equippedHouseFolder:GetChildren()) do
		child:Destroy()
	end

	-- Clone the purchased house model to the player's inventory
	local houseModel = game.Workspace[houseName]:Clone()
	houseModel.Parent = equippedHouseFolder

	-- Additional logic to handle other equipping functionality, if needed
end

-- Connect the "Buy" button of each house to the purchaseHouse function
local defaultHouseButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House1["BUY 1"] -- Reference to the "Buy" button for Default House
local defaultEquipButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House1Equip.equipButton -- Reference to the "Equip" button for Default House
defaultHouseButton.MouseButton1Click:Connect(function()
	purchaseHouse("DefaultHouse", 0, defaultHouseButton, defaultEquipButton) -- Pass the house name, price, and buttons as arguments
	end)

local beginnerHouseButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House2.BUY -- Reference to the "Buy" button for Beginner House
local beginnerEquipButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House2Equip.equipButton -- Reference to the "Equip" button for Beginner House
beginnerHouseButton.MouseButton1Click:Connect(function()
	purchaseHouse("BeginnerHouse", 2000, beginnerHouseButton, beginnerEquipButton)
end)

local advancedHouseButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House3["BUY 1"] -- Reference to the "Buy" button for Beginner House
local advancedEquipButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House3Equip.equipButton -- Reference to the "Equip" button for Beginner House
advancedHouseButton.MouseButton1Click:Connect(function()
	purchaseHouse("AdvancedHouse", 5000, advancedHouseButton, advancedEquipButton)
end)

local proHouseButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House4["BUY 1"] -- Reference to the "Buy" button for Beginner House
local proEquipButton = game.StarterGui.Shop2.CloseFrame.BuildingsFrame.House4Equip.equipButton -- Reference to the "Equip" button for Beginner House
proHouseButton.MouseButton1Click:Connect(function()
	purchaseHouse("BeginnerHouse", 10000, proHouseButton, proEquipButton)
end)

-- Connect the other house buttons in a similar manner

-- Connect the "Equip" button of each house to the equipHouse function
defaultEquipButton.MouseButton1Click:Connect(function()
	equipHouse("DefaultHouse") -- Pass the house name as an argument
end)

beginnerEquipButton.MouseButton1Click:Connect(function()
	equipHouse("BeginnerHouse")
end)

advancedEquipButton.MouseButton1Click:Connect(function()
	equipHouse("AdvancedHouse")
end)

proEquipButton.MouseButton1Click:Connect(function()
	equipHouse("ProHouse")
end)
-- Connect the other equip buttons in a similar manner

P.S. I am pretty new to scripting, this is the only game I ever worked on. Last time I coded was about a year ago.

I’m sorry, this isn’t really a fix but I’d like to note none of this would transfer to the server and is all client side, by the looks of it. Furthermore, do not use ChatGPT for any of your code. It may seem cool but 90% of the time the code is either:

  • Outdated
  • Made-up (E.G Made up in-built functions, variables, properties, etc.)
  • Laggy/Not production ready - None of this code should be used in actual games as it’s never usually good for performance, could be potentially buggy or could just outright not work.

This is all important code practises that will just help your code look and perform better.

Your fix appears to be in the equipButton and perhaps more, but you’re searching for game.StarterGui which is not correct. StarterGui contains instances that get moved into the Player’s PlayerGui and does not get touched again after moving instances. You should use (Player:FindFirstChildOfClass(“PlayerGui”) or Player:WaitForChild(“PlayerGui”))

1 Like

Exactly why I was scared to use ChatGPT in the first place haha. I didn’t know about the PlayerGUI part, I will try fixing it now.

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