How to make a GUI fade in/out when a player is touching a certain part?

Could you mark my post as the solution?

So I would highly advise against using the server-side to tween UI. I would recommend using the client.

Here is a client script, you will need to tweak it to your liking but it will work as I have tested it while creating it.

--Services
local players = game:GetService("Players")
local tweenService = game:GetService("TweenService")

--Constants
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui", playerGui)
local frame = Instance.new("Frame", screenGui)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.Size = UDim2.new(0.5, 0, 0.5, 0)
frame.Position = UDim2.new(0.25, 0, 0.25, 0)
frame.BackgroundTransparency = 1

--Variables
local part = workspace:WaitForChild("Part", 86400)
local debounce = false
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

--Functions
local function Action(type)
	local goals = {}
	if type == "FadeIn" then
		goals.BackgroundTransparency = 0
	elseif type == "FadeOut" then
		goals.BackgroundTransparency = 1
	end

	local tween = tweenService:Create(frame, tweenInfo, goals)
	tween:Play()
end

local function onTouchStart(hit)
	if debounce then return end
	if hit.Parent:FindFirstChild("Humanoid") then
		debounce = true
		Action("FadeIn")
	end
end

local function onTouchEnd()
	if not debounce then return end
	debounce = false
	Action("FadeOut")
end

part.Touched:Connect(onTouchStart)

--
runService.RenderStepped:Connect(function()
	if debounce then
		local touching = false
		for _, obj in ipairs(part:GetTouchingParts()) do
			if obj.Parent:FindFirstChild("Humanoid") then
				touching = true
				break
			end
		end
		if not touching then
			onTouchEnd()
		end
	end
end)

Let me know how this one works for you. Obviously I have made it very plain so as I said before, please tweak it to your liking.

Edit: Forgive my usage of a while task.wait() loop. Use RunService, please.

1 Like

Oh! Okay well do not use touched events. They overall suck. Here though!

local CS = game:GetService("CollectionService")
local RS = game:GetService("RunService")

local overlapparam = -- overlapparams to filter out anything that you dont want

RS.RenderStepped:Connect(function()
    for i,v in pairs(CS:GetTagged("DeathPart")) do
        local touchingparts = workspace:GetTouchingParts(v,overlapparam)
        for _,v2 in pairs(touchingparts) do
            local h = v2.Parent:FindFirstChild("Humanoid") or v2.Parent.Parent:FindFirstChild("Humanoid")
            if h then
                -- make gui die
            end
        end
    end
end)

There may be bugs because it is not tested and I wrote this in devforum so yea.

1 Like

Your use of RenderStepped is what I should have done with my reply, lol. I’ll quickly make an edit.

Alrighty, just give me a second to test and review everyone’s answers! I appreciate y’all’s time and help.

We already went over that, we used tween service on the client

This script doesn’t require a ModuleScript or a Remote Event, which is why I felt the need to post it.

When did we mention Module scripts? A remote event is much more understandable. it can be fired as soon as the player touches the part instead of having a RenderStepped function.

I used that approach as an example to demonstrate how function variables can be passed, creating what resembles a pseudo-service. In programming, the goal is often to achieve outcomes using as few lines of code as possible. Therefore, employing a remote event for detecting block touches is ultimately unnecessary when this can be efficiently handled on the client side, eliminating the need for server involvement.

Pf_sz1’s script does spawn the GUI but unfortunately doesn’t make it fade out once you’re no longer touching the block. Xurko, yours does exactly what I need! Haven’t tested Night’s yet since it appears to be a bit incomplete, I imagine either they forgot to add stuff or I need to replace some of the bits with my own code. Either way, I thank you all for helping me with this!! Now all I gotta do is transition the code to work with the tag system so I don’t have to copy and paste the code in every single part I want to have this effect.

I also took videos so people can see how your codes work, just in case it might be helpful for them.

pf_z1:

Xurkostaff:

Could you please mark my post as the solution if you feel your request has been fulfilled?

I believe I have, let me know if I’m wrong.

That was my bad, I needed to refresh the page, thank you.

Hey! Stealing my idea!! >:(

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