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

How to remove gui from playergui when a part is touched?

I've been trying to do this for about 2 hours now and I just can't seem to find the answer that I'm looking for. Hopefully, someone helps me with this.

here's my script:

local Playergui = game.Players.LocalPlayer.PlayerGui
function onTouch(part)
	if Playergui.ScreenGui then
		Playergui.ScreenGui:Destroy()
	end
end
	
script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

You can't get a localplayer from a normal script.

if you want to get a player from a character, use GetPlayerFromCharacter Link The name is self-explanatory but if I have to tell you what it does, basically it gets a player from a player's character.

function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") then -- Checks if the part has a humanoid, if it has, it's a player (or an npc,)
		local Player = game.Players:GetPlayerFromCharacter(part.Parent) -- The parameter is the player's character
		local PlayerGui = Player.PlayerGui.ScreenGui:Destroy()
		
	if PlayerGui.ScreenGui then
		PlayerGui.ScreenGui:Destroy()
		
		end
	end
end

script.Parent.Touched:connect(onTouch)

Not sure if there's an better way to do this, but I hope this helps.

1 comment

0

It worked! Thanks!

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