TextButton not working

I’m making a car delete UI , but the actual TextButton doesn’t delete the car when clicked.

I’ve tried using Instance.Activated as well as .MouseButton1Click, but neither work.

local Players = game:GetService("Players")

local DeleteCarUI = Players:WaitForChild("PlayerGui").DeleteCarUI
local DeleteButton = DeleteCarUI.Frame.TextButton

local function onActivated()
	script.Parent.Parent.Parent.Parent:Destroy()
end

DeleteButton.MouseButton1Click:Connect(onActivated)

https://gyazo.com/06c3b63c6fceb9c3fe939b0861cb9dba

Are there any errors appearing, and can you show us your Explorer?

Because you’re trying to get PlayerGui from the players service instead of a player on line 3.

Nope, no errors showing.
image

So I should make player a variable and do player:WaitForChild(...)?

What @BenMactavsin said was correct. If this is client-sided (and I assume it is), get the LocalPlayer

local Players = game:GetService("Players").LocalPlayer

Yes, you should reference to the player who spawned the car and use :WaitForChild() on that player.

But am I right in saying you can only retrieve LocalPlayer from a LocalScript?

Correct. Is this a server script? I wouldn’t use them on UI objects and instead use events

This is the same issue I ran into with my client. I have no idea how their spawners work, so I’m not sure how to retrieve the person whop spawned it, so I’m just doing the current player in the seat.

Okay, I’ll move it to a LocalScript and use your method.

So my final code looks like this
@BenMactavsin

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local DeleteCarUI = LocalPlayer:WaitForChild("PlayerGui").DeleteCarUI
local DeleteButton = DeleteCarUI.Frame.TextButton

local function onActivated()
	script.Parent.Parent.Parent.Parent:Destroy()
end

DeleteButton.MouseButton1Click:Connect(onActivated)

In a LS

If your trying to delete a car using a GUI, you’ll have to use a RemoteEvent. Here’s something I made.

LocalScript (Place in GUI)

local Player = game.Players.LocalPlayer

local DeleteCarUI = script.Parent
local DeleteButton = DeleteCarUI.Frame.TextButton

DeleteButton.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

Server Script

local event = game.ReplicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(player)
	local findseat = player.Character.Humanoid.SeatPart
	if findseat then
		player.PlayerGui.DeleteCarUI.Enabled = false
		findseat.Parent.Parent.Parent:Destroy()
	end
end)

You may have to change some of the instances to delete the car, since it’s using the Seat from earlier to troubleshoot your previous issue. But this should look right from your explorer.

the player gui is a child of the local player, not the players service. you need to get the local players gui (perhaps from the occupant property?)

local Player = game.Players.LocalPlayer

local DeleteCarUI = Player:WaitForChild("PlayerGui")
local DeleteButton = DeleteCarUI.Frame.TextButton

DeleteButton.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.DeleteJeepEvent:FireServer()
end)
10:21:17.014  Workspace.PolLand.Body.ARV#5751914.VehicleSeat.DeleteUIActivate:3: attempt to index nil with 'WaitForChild'  -  Server  -  DeleteUIActivate:3

This error appears for the localscript

Like I said, that needs to be in a local script under the GUI. Once you do that just replace the DeleteCarUI variable with script.Parent.

Replace the ServerScript with this, it could also work without the script needing to be in the vehicle.

local event = game.ReplicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(player)
	local findseat = player.Character.Humanoid.SeatPart
	if findseat then
		player.PlayerGui.DeleteCarUI.Enabled = false
		findseat.Parent.Parent.Parent:Destroy()
	end
end)

Do I replace SeatPart with the name of the seat (VehicleSeat)?

Nope, it’s a variable in the Humanoid that updates if the Character’s Humanoid is sitting in a seat or not. Changing it will just result in an error.

This one worked great thanks for all the replies!

Only problem now is this

It causes the player to glide.
I forced the player to jump before it deleted the car, but that didn’t work.