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.
Hopefully somebody can identify the issue, thank you.
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.
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)
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.
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.
Here’s the studio file, hope this is the solution to your problem. flashlighttest.rbxl (79.1 KB)