Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Filteringenabled - can't access PlayerGui?

I'm currently converting my game to use filteringenabled and I ran into a slight problem here. Basically what i'm trying to do is when a player clicks a gui, it will remove the players car from workspace and the cars gui's; which is just some controls for the car; from the player, but the script can't access PlayerGui to remove the car's gui!

here are the scripts:

I have a local script in the gui

script.Parent.MouseButton1Down:connect(function()
	game.Workspace.Events.RemoveCar.Clicked:FireServer()
end)

and a normal script in a folder which is in workspace with a remoteevent called "clicked" in it

script.Clicked.OnServerEvent(function(player)
	local found = false
	for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
		if obj:FindFirstChild("user") then
			if obj.user.Value == player.Name then
				found = true
				obj:remove()
			end
		end
	end
	local gui = player.PlayerGui:FindFirstChild("Display")
	if gui then
		gui:remove()
	end
	if found == false then
		player.PlayerGui.carRemove.TextButton.Text = "You have none!"
		wait(2)
		player.PlayerGui.carRemove.TextButton.Text = "Remove Car"
	end
end)

the problem is, the script can't access the players PlayerGui and I cannot find a way around this!

2 comments

0

FireClient

0

if I use fireclient it's going to say in output that it can only be called from server

1 answer

✓ Accepted answer

Instead of removing the GUI from server side, delete it directly from the event in your local script.

Just because changes won't replicate to server, it doesn't mean you can't create objects or destroy them from client side. It's actually very benifitial to do so and it makes sense in this case, since UI has nothing to do with other server, it's completely local feature.

This is how it could look.

The local script:

script.Parent.MouseButton1Down:connect(function()
	game.Workspace.Events.RemoveCar.Clicked:FireServer()
	
	-- Use same loop in client script, just don't delete the car
	local found = false
	for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
		if obj:FindFirstChild("user") then
			if obj.user.Value == game.Players.LocalPlayer.Name then
				found = true
			end
		end
	end

	local gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("Display")
	if gui then
		-- Use destroy instead of remove. Remove only sets the object's parent to nil, and might not get garbage collected if something is still accessing the object
		gui:Destroy()
	end

	if found == false then
		game.Players.LocalPlayer.PlayerGui.carRemove.TextButton.Text = "You have none!"
		wait(2)
		game.Players.LocalPlayer.PlayerGui.carRemove.TextButton.Text = "Remove Car"
	end
end)

The server script:

script.Clicked.OnServerEvent(function(player)
	-- All the server should do is destroy the car
	for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
		if obj:FindFirstChild("user") then
			if obj.user.Value == player.Name then
				obj:Destroy()
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.