I am currently experiencing issues with a script that I am unable to decipher why it is not working.
I am trying to have a script that changes the text of a textbutton when certain parameters are met. Here’s an image of the script.
Please excuse my weird script editor colors. I use this script for 3 different gui textbuttons that when clicked spawn a car. The script only changes the text for the first button clicked, and then changes the script to the “buy ~ 15,000” for the other buttons, not changing it to SPAWN like I want. I eventually plan on adding a cost/buy system, but for now I just want to make it change to spawn.
Ok so there’s some weird stuff going on. The editor doesn’t say there’s any errors, the cars spawn normally, but the text still doesn’t work for any of the buttons except for the first one pressed, and when I look in the output, there’s some random sound that’s not even for sale. There are no sounds in my model, or even my game. It sends a new error whenever I click the button. Here’s what it looks like:
local player = script.Parent.Parent.Parent.Parent.Parent.Parent
local model = game.ServerStorage:WaitForChild("Charger")
script.Parent.MouseButton1Click:Connect(function()
-- should this be the first line when player clicked? to check if car needs to spawn or be purchase?
if script.Parent.Text == "SPAWN" or script.Parent.Text == "Spawn" then
script.Parent.Text = "Spawn"
else
script.Parent.Text = "Buy ~ 15,000"
end
-- do a check if player own car first before spawning it
if script.Parent.Text == "Buy ~ 15,000" then
--prompt the purchase
-- here's an example to continue making this work since it's just a trial
print("player being showned a gui to purchase and they pressed the button to buy")
print("player's currency got deducted for buying car")
script.Parent.Text = "SPAWN"
return end -- to not continue to spawn the car because needs to purchase
if script.Parent.Text == "SPAWN" or script.Parent.Text == "Spawn" then
--make the spawn
if workspace:FindFirstChild(player.Name.."'s Car") ~= nil then
workspace:FindFirstChild(player.Name.."'s Car"):Destroy()
local clone = model:Clone()
clone.Name = player.Name.."'s Car"
clone.Parent = workspace
--set the model's position, play with numbers so car won't spawn inside floor or too close to player pushing player off map
clone:MoveTo(player.Character.PrimaryPart.CFrame.LookVector * 18 + Vector3.new(0,7,0))
script.Parent.Text = "Spawn"
elseif workspace:FindFirstChild(player.Name.."'s Car") == nil then
local clone = model:Clone()
clone.Name = player.Name.."'s Car"
clone.Parent = workspace
--set the model's position
clone:MoveTo(player.Character.PrimaryPart.CFrame.LookVector * 18 + Vector3.new(0,7,0))
script.Parent.Text = "Spawn"
end
end
end)
I didn’t realize this before since your indentation was a bit confusing, but I think it may be because you didn’t end your first if statement before starting your second one, which could be causing some problems.