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

GUI only popping up when the part is touched?

I only want my gui to show when the part is being touched, right now it is staying on the screen until you X out of it. This is what I have using a remote event(probably wrong thing to use)

local limit = 2
 
script.Parent.Touched:Connect(function(hit)
	    if hit.Parent:FindFirstChild("Humanoid") then
	       
	        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	        if player.leaderstats.Points.Value >= limit then
	           
	            if hit.Parent:FindFirstChild("Torso") then
	                script.Parent.CanCollide = false
	            elseif hit.Parent:FindFirstChild("UpperTorso") then
	                script.Parent.CanCollide = false
	           
	        end
	    end
	end
end)
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		game.ReplicatedStorage.Popup:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
	end
end)

6 comments

0

u s e c o d e b l o c k s f o r y o u r e n t i r e s c r i p t

0

ok i dont get it when do you want it to pop up? because its like its not popping up at all? what's your problem xd

0

it pops up when a part is touched but I only want it to show when the part is being touched, right now it stays after you stop touching the part

0

Why are you even using :FireClient()? The player GUI can be accessible by doing game.Players:GetPlayerFromCharacter(hit.Parent). Bruh

0

im new to scripting

0

if you want it to pop up when touched, do script.Parent.Touched, and when you want it to go away, use script.Parent.TouchEnded.

1 answer

✓ Accepted answer

Your solution to this is TouchEnded. CanCollide has nothing to do with stopping a touched event, so don't use that. This is what you should be using

Part.Touched:Connect(function(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) then
		-- Fire a client event to pop up a GUI
	end
end)

Part.TouchEnded:Connect(function(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) then
		--Close the GUI with a client event
	end
end)

A better solution would be to use magnitude from a local script. It will work nicely if it's a cylinder too. That would work like this

local RunService = game:GetService('RunService')

local Range = 10 --Distance in studs
local Part = game.Workspace.Part --Part that you want to detect when touched
local LocalPlayer = game.Players.LocalPlayer

repeat wait() until LocalPlayer.Character

local PlayerCharacter = LocalPlayer.Character
local HumanoidRootPart = PlayerCharacter:WaitForChild('HumanoidRootPart')

RunService.Heartbeat:Connect(function() --fires every frame/FPS
	if (HumanoidRootPart.Position - Part.Position).magnitude < Range then
		--Pull up the GUI from here

	else
		--Close the GUI here
	end
end)

Hope this helps out.

2 comments

0

Where do I put the local script? StarterCharacterScripts?

0

also does this work on a normal part because I can't make it work

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