Car Spawning script not working

Hello!

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.

Thanks!

1 Like

use game.players.LocalPlayer instead of
image

1 Like

Are there any messages in the console? If so please send them here

Also like @DevLazl said, use game.Players.LocalPlayer instead of that mess if this is a local script

1 Like

You can’t access ServerStorage through LocalPlayer, as LocalPlayer can only be used in a local script

1 Like

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:


This is the only thing showing up in the output btw
Thanks!

I assumed this was a local script because they were manipulating a textbutton -
image

yeah this is def a local script my man

It’s not a local script. If I use a local script nothing works at all. I’m just using a normal script.

and what is the script’s parent?

1 Like

The text button is the parent. extra text to reach minimum

You should probably just use remote events, it will make things a lot less jenky

1 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)
1 Like

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.

Try reading up on elseif statements here: Control Structures | Roblox Creator Documentation

1 Like

Thank you so much!!! Works like a charm!

1 Like

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