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

I’m basically trying to make it so that when a player is touching a certain block, a GUI (which will consist of a single, transparent image or background color) will slowly fade on to their screen. Once said GUI is fully on their screen, they’ll die. And when they’re no longer touching the part, it’ll fade back out.

I imagine I’ll need a timer or a way to detect when the GUI is at its minimum transparency. I already know how to make it detect when a player is touching it and how to make a GUI fade in/out. I’m just unsure on how to accomplish an effect like this with what I already know.

2 Likes

You would fire a remote event when the player touches a part and then you would use Tween service to a GUI fade.

Server script: (PUT THIS IN THE PART)

local Part = script.Parent
local Event = game.ReplicatedStorage.EVENT --Make a new remote event and put it in Rep Storage. Change 'EVENT' to the name of the event


Part.Touched:Connect(function(hit)

   local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

   if plr then --Checks if it is a player that touched the part

       Event:FireClient --Fires the RemoteEvent. We dont want to tween on the client because tweens 
       done on the client reduce strain on the server

   end

end)

Client Script: (Put this in the GUI or something)

local Frame = script.Parent
local Event = game.ReplicatedStorage.EVENT --Change 'Event' to the name of your event
local TweenService = game:GetService("TweenService")


local FadeInTween = TweenService:Create(Frame, TweenInfo.new(0.5, Enum.EasingStyle.Quad, EasingDirection.In, {['BackgroundTransparency'] = 1}) -- Fade in tween
local FadeOutTween = TweenService:Create(Frame, TweenInfo.new(0.3, Enum.EasingStyle.Quad, EasingDirection.Out, {['BackgroundTransparency'] = 1}) -- Fade out tween

Event.OnClientEvent:Connect(function() --Happens when the player touches the part (we fired a client event)

    FadeInTween:Play() --Plays the tween (Fade in)
    FadeInTween.Completed:Wait() --Waits until the tween is completed then moves onto the next task

    FadeOutTween:Play() --Plays the Fade Out tween (You can delete this or do whatever you want with it. 

end)

Pretty simple stuff, im sure I made a few spelling mistakes here, so let me know if it works, if it doesn’t, I will help you out.

Also if you need me to explain anything else, let me know, I didnt go into detail with TweenService, check out: TweenService | Documentation - Roblox Creator Hub if you want to know more about TweenService.

Let me know if this works, Enjoy.

2 Likes

Let me know if it works or if you need help with anything

1 Like

Just tested out in-game (with small fixes to both of the scripts), but it unfortunately seems like it isn’t working just yet. Not sure if it’s the script itself or if my GUI is making it bug out (or if my fixes broke it), we’ll see.

Server Script:

local Part = script.Parent
local Event = game.ReplicatedStorage.Event --Make a new remote event and put it in Rep Storage. Change 'EVENT' to the name of the event


Part.Touched:Connect(function(hit)

	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr then --Checks if it is a player that touched the part

		Event:FireClient(plr) --Fires the RemoteEvent. We dont want to tween on the client because tweens done on the client reduce strain on the server

	end

end)

Client Script:

local Frame = script.Parent.Frame
local Event = game.ReplicatedStorage.Event --Change 'Event' to the name of your event
local TweenService = game:GetService("TweenService")


local FadeInTween = TweenService:Create(Frame, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false), {BackgroundTransparency = 1})
local FadeOutTween = TweenService:Create(Frame, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false), {BackgroundTransparency = 1})

Event.OnClientEvent:Connect(function() --Happens when the player touches the part (we fired a client event)

	FadeInTween:Play() --Plays the tween (Fade in)
	FadeInTween.Completed:Wait() --Waits until the tween is completed then moves onto the next task



end)
1 Like

Is there anything in the output? (Press F9) and screenshot it

1 Like

There doesn’t appear to be much aside from Adonis loading as well as an unrelated EmoteGUI I never finished lol.

1 Like
Event.OnClientEvent:Connect(function()

After this line, put: print("This works")

If it prints then we are on the right path, if it doesn’t then I will try something else

1 Like

Alrighty, here’s what it says now. Better than getting nothing on the output.

Screenshot (1933)

I think you mixed up the scripts. ‘OnClientEvent’ should be in the local script


This should be in a local script^

On line 10 (In the local script), put: print('It works')
Screenshot the output when you test it

Ah, I would do something like that lol. Sorry, I’m a bit tired so my brain isn’t all there. Fixed it and got the desired result.

Screenshot (1934)

Np, it happens

Try this: (Copy and paste it into your local script)

local Frame = script.Parent.Frame
local Event = game.ReplicatedStorage.Event --Change 'Event' to the name of your event
local TweenService = game:GetService("TweenService")


local FadeInTween = TweenService:Create(Frame, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {BackgroundTransparency = 1})

Event.OnClientEvent:Connect(function() --Happens when the player touches the part (we fired a client event)

	FadeInTween:Play() --Plays the tween (Fade in)

end)

Wait mb, I am also tired. I forgot to make the Frame visible lol. This should work:

local Event = game.ReplicatedStorage.Event --Change 'Event' to the name of your event
local TweenService = game:GetService("TweenService")


local FadeInTween = TweenService:Create(Frame, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {BackgroundTransparency = 1})

Event.OnClientEvent:Connect(function() --Happens when the player touches the part (we fired a client event)
    Frame.Visible = true
    Frame.BackgroundTransparency = 0
	FadeInTween:Play() --Plays the tween (Fade in)

end)

This at least pops up the GUI which means we’re going in the right direction! Mild flashing warning for the video, I just wanted to show what’s happening with this script in place.

Why use events at all btw? Why not just add a tag in each part you want to do this for and use the client and collection service? Won’t that be wayyy better?

Oh yeah, my bad I forgot to add a cooldown. (I am very tired lol)

We will add the cooldown on the client so it doesnt effect the server.

Client script:

local Event = game.ReplicatedStorage.Event --Change 'Event' to the name of your event
local TweenService = game:GetService("TweenService")

local cd = false
local FadeInTween = TweenService:Create(Frame, TweenInfo.new(0.55, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {BackgroundTransparency = 1})

Event.OnClientEvent:Connect(function() --Happens when the player touches the part (we fired a client event)
    if cd == false then
        cd = true
    	FadeInTween:Play() --Plays the tween (Fade in)
        FadeInTween.Completed:Wait()
        cd = false
    end
end)

This should work

1 Like

I am going to be using the tag system after getting a solution, I thought I had mentioned it in the post, sorry about that. I just thought it might be easier to see how someone would do it with normal scripts and then replicate it myself with the tag and collection service.

So why even make this topic in the first place if you already know how to do this? Or I understand you wrong?

I know how tags and the collection system works, I’m just not sure how to accomplish the effect I talk about in the post (a GUI fading in and out depending on if the player is touching it or not.). Basically I know how these things work separately but not combined together. Sorry if it’s confusing!