Code can not find a GUI in PlayerGui

I have currently having an issue where my code can not find a GUI in PlayerGui when that GUI is in PlayerGui. Currently I have a part with a proximity prompt and when the player interacts wuth the proximity prompt it should scan if the player has a Gui named “FlashlightButton” in their PlayerGui. And if the player does, then it will be destroyed. But the problem is, even if the GUI is in the PlayerGui nothing happens.

I have a GUI being cloned to PlayerGui for each player when they join the game. I also have a proximity prompt with a script.

Here is the script code:

local toolDestroyPrompt = script.Parent.ProximityPrompt
local promptRange = 10 

toolDestroyPrompt.MaxActivationDistance = promptRange

toolDestroyPrompt.Triggered:Connect(function(player)
	
	local playerGui = player:FindFirstChild("PlayerGui")

	if playerGui then
		local FlashlightButton = playerGui:FindFirstChild("FlashlightButton")
		if FlashlightButton then
			FlashlightButton:Destroy()
		end
	end



	if player.Character and player.Character:FindFirstChildWhichIsA("Tool") then
		player.Character:FindFirstChildWhichIsA("Tool"):Destroy()
		script.Parent.Throw:Play()
	end
end)

Here is PlayerGui once player joins the game. Which as you can see has the FlashLightButton GUI.

image

Hopefully somebody can identify the issue, thank you.

1 Like

Is the script server or local sided ?

If the gui is being cloned on the client, but your proximity prompt code is on the server - the cloned gui will not exist.

To fix this move your server-side script for the proximity prompt into a localscript. Fix any errors and reference the player through game.Players.LocalPlayer.

1 Like

The script above is server sided.

Could be that the server side doesn’t see your UI since the UI could be local sided. Like what @CoIorEvent mentioned above.

I threw that code into a local script and added a Local Player variable but it still does not work.

local toolDestroyPrompt = script.Parent.ProximityPrompt
local promptRange = 10
local player = game.Players.LocalPlayer

toolDestroyPrompt.MaxActivationDistance = promptRange

toolDestroyPrompt.Triggered:Connect(function(player)
	local playerGui = player:FindFirstChild("PlayerGui")

	if playerGui then
		local FlashlightButton = playerGui:FindFirstChild("FlashlightButton")
		if FlashlightButton then
			FlashlightButton:Destroy()
		end
	end

	if player.Character and player.Character:FindFirstChildWhichIsA("Tool") then
		player.Character:FindFirstChildWhichIsA("Tool"):Destroy()
		script.Parent.Throw:Play()
	end
end)

have you done prints to check if each statement works. Like if the prompt gets triggered or if the flashlightbutton gets found ?

Yes, I added a few but nothing printed at all. It seems nothing happens at all when the proximity prompt is being interacted with. All my variables look correct and I do not notice any spelling mistakes.

Then there’s your problem if you want I could fix it for you if you give me studio access or show me where your prompt is located.

Here is the place file, the GUI is cloned once the player equips the tool:
flashlighttest.rbxl (79.8 KB)

Alright so I have done a few modifications on two of your scripts.

On your flash light I’ve added a clean up function so when it gets deleted your Ui will be deleted

And noticed that your tool destroyer doesn’t destroy the flashlight in your backpack so I added a extra check to make sure if its not in the character, check the backpack.

image

Here’s the studio file, hope this is the solution to your problem.
flashlighttest.rbxl (79.1 KB)

1 Like

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