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