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

How do I delete this gui?

I have two scripts:

function onTouched(Hit)
	if Hit.Parent.Humanoid ~= nil then
		name = Hit.Parent.Name
		print("Name found")
		gui = script.Parent.AccessBlocked:Clone()
		par = game.Players:FindFirstChild(name)
		gui.Parent = par.PlayerGui
		print("gui parent changed")

	
	end
end

script.Parent.Touched:connect(onTouched)

function onTouched(Hit)

if Hit.Parent.Humanoid ~= nil then
	name = Hit.Parent.Name
	par = game.Players:FindFirstChild(name)
	
	if par.PlayerGui:FindFirstChild("AccessBlocked") then
		thescript = par.PlayerGui:FindFirstChild("AccessBlocked")
		wait(.5)
		thescript:Destroy()
end	
end
end
script.Parent.Touched:connect(onTouched)

The scripts have the same parent: a part in workspace The part has 3 children: the two scripts and a gui named AccessBlocked

I'm trying to make an invisible wall that when I touch, it shows a gui on my screen saying "Out of Bounds". It works except for the deleting part! This is the weird part: It sometimes does delete the gui but only if the wait() isn't present. It isn't deleting it at all.

1 comment

0

Sometimes :Destroy() does not work. (I don't know why I just haven't looked into it) but try replacing :Destroy with :Remove() it does the same task!

1 answer

✓ Accepted answer

FindFirstChild returns nil if it doesn't find the child straight away, so your script may not have found your GUI, so you will need to use WaitForChild. WaitForChild essentially makes the script yield until the child you're waiting for is available.

To fix your script, just replace FindFirstChild with WaitForChild and get rid of the wait after it and the deleting part should work fine.

Also, I would suggest using GetPlayerFromCharacter as it is much more efficient than finding the name of the character as a model could be the same name as the player in question.


Script 2 Code:

function onTouched(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		plr = game.Players:GetPlayerFromCharacter(Hit.Parent) --Use GetPlayerFromCharacter to find the player. 
		if not plr then return end --If there is no player connected to the character, stop the function.
		thescript = par.PlayerGui:WaitForChild("AccessBlocked") --Waits for the GUI to appear.
		thescript:Destroy()
	end
end
script.Parent.Touched:connect(onTouched)

I hope my answer helped you. If it did, be sure to accept it.

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