I need help; when a person touches an object, a screen UI text fades in and out

mm thats true. You could also do it based on position. I would still personally say that the part is the best way to do it if we can make it work, but if we cant, that is def a good way we could do it.

1 Like

Ohhh i see. You need to put the local script in starter player > starter player scripts

1 Like

Well, I can’t work on it today since I have to go to bed. I’m still going to keep you updated about this situation. Thank you so much for taking your time to help me!

This is an off-topic question but do you have discord by chance? It’s really difficult for me to keep up with DMs since I use DevForum occasionally.

Ok, ttyl then! Whats ur discord, I can add you whenever. Yea np, its fun to do!

1 Like

Here we go again…

Why do people think StarterGui updates UI for the player? It doesn’t. The only purpose of StarterGui is to copy UIs that you made over to the PlayerGui folder under the player object.

Here’s your code, but done properly. Please read my notes for a better understanding on how these work. I’m not doing this in studio so if there’s an error, lemme know.

-- We cannot get the Players UI because the function hasn't been ran yet, so we cannot put the UI segment here.

-- What is the part that ``Touched:Connect(function()`` is being used on? We need to specify that.
local touch = script.Parent -- I'm assuming the script is a Child of the Part.

-- We need to get TweenService to properly tween in our UI. using Repeat until is not efficient when theres a service for this use case
local TweenService = game:GetService("TweenService")

-- Get the Players Service so we can find the player when they touch the part --
local PlayersService = game:GetService("Players")

-- Touched runs every time a player touches the part! Ouch! To prevent this, put a debounce (huge lag can occur if you don't!)
local db = false

touch.Touched:Connect(function(Part) -- we need to specify the part that hit it so we can define the User UI later.
    if not db then -- check to see if the debounce is false, if it is immediately set it to true so the script won't run again until it's declared false again
        if Part.Parent:FindFirstChild("Humanoid") then -- We need to put this here so our code doesn't execute when another part that isn't a player touches this
            db = true -- this stops runs every touch

            local Humanoid = Part.Parent:FindFirstChild("Humanoid") -- We want to locate our Humanoid for a very specific reason
            local Player = PlayersService:GetPlayerFromCharacter(Humanoid.Parent) -- We can get the Player to access their GUI.

            local UI = Player.PlayerGui:WaitForChild("NameOfYourUIHere") -- simply replace the name of this string with the name of your ScreenGui
            local MapLabel = UI:WaitForChild("MapTitle") -- Finally we can get the TextObject so we can tween it to full transparency

            MapLabel.Text = "MapTest1" -- Call this whatever the name of the map is that you stepped into

            TweenService:Create(MapLabel, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {TextTransparency = 0}):Play() -- What this does is makes a tween that makes the UI fade in.

            task.wait(3) -- an improved version of wait().

            TweenService:Create(MapLabel, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 1}):Play() -- What this does is makes a tween that makes the UI out in.

            db = false -- set the debounce back to the false so the code can execute again
        end
    end 
end)

task.wait(0.01) -- changed to task.wait() for your own convenience.
touch:Destroy() -- unsure why you would want to destroy the part..? if you destroy it, it won't ever tell you the name of the map again!

I’m assuming this is a ServerScript. If not, change this line

local touch = script.Parent

to

local touch = workspace:WaitForChild("NameOfPartHere")

and move the script to StarterPlayerScripts under StarterPlayer in the explorer for this to work client sided.

3 Likes

The OP has already changed StarterGui to PlayerGui. Did you read the posts all above you?
You should, mostly since you miss stuff, such as that the script was already turnt into a LocalScript. You can just see the code and notice because of the “player.LocalPlayer” line.

Anyways, your idea is great, I didn’t think of tweening.

You should, I’ve seen many questions left unanswered because of not testing the answers. But the code seems right. And I like that you have suggested task.wait()

Also, you should instead try to ask the OP to update the code, or figure out the current code yourself.

2 Likes
local amount = 3

game:GetService("Players").PlayerAdded:Connect(function(plr)
      local UI = plr.PlayerGui.LocationToYourGui
-- make sure to define the ui that you want to fade in and out, I called it "Frame" in this example
      game:GetService("TweenService"):Create(Frame, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {BackgroundTransparency = 0}:Play()
      wait(amount)
      game:GetService("TweenService"):Create(Frame, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {BackgroundTransparency = 1}:Play()
end)
1 Like

I don’t think that type of problem shows up. I think that the “player” variable wasn’t declared.
The first lines should be:

local Players = game:GetService("Players")
local player --we declare the variable because it's gonna be nil otherwise (bugs)

touch = game.Workspace:WaitForChild("touch") --change this to find your part

touch.Touched:Connect(function(hit)
	
	local character = hit.Parent
	
	if character then
		player = Players:GetPlayerFromCharacter(character) --we're requesting it, not declaring it. again, studio thing.
	else
		warn("character not found")
	end

I’m not 100% sure this is correct, but I think so.

1 Like

Just wondering, why would you make the player touch a part when they spawn instead of using something like CharacterAdded or PlayerAdded to check when a player spawns?

2 Likes

The OP requested it with a specific condition: when touching a part. Not when joining.
Could you at least elaborate on what you did on your code instead of putting a code snippet without further explanation? Some don’t understand the code without explanation.

You see, sometimes you want it to work when your character hits it and not when your character spawns. Something like Minecraft Survival servers, you go away and you get a message saying PVP is on. You can’t do that with CharacterAdded, and even less with PlayerAdded.

I would not suggest PlayerAdded anyways, as while you’re loading/not spawned yet the code is executing.

1 Like

Wait may be a bad practice, but in situations like this, I find it rather helpful.

2 Likes

I’m also pretty sure that OP wants to create some sort of message when the player joins, like in FPS games. When you join the map it states the name and maybe an image of it in the bg.


Jeez… That took me a while to find

2 Likes

I actually just tested my code in studio and revised some minor mishaps I made but it works completely fine, and does exactly what the OP wants. I apologize if my post came off quite rude.

I didn’t throughly read all the replies because I wanted to compile a mass amount of generalized ideas (things that people typically send on topics like these) and put it all into one script, and leave some notes so the person asking the topic question so they can grasp easier concepts, and overall, more efficient code.

I’ve had similar issues in the past, so I figured the way I did it would be different from everyone else.

Thank you for reading.

3 Likes

Because, people can go to several maps on the same place. Everytime they go on a map, the text of the map shows up.

Makes sense, though don’t you think region3 might be a better solution for this?

1 Like

That’s alright! As I said before, I am a terrible scripter (a rookie) and can only do simple stuff. Since I’m on phone right now, I’ll test the code in a minute.

2 Likes

I never really heard of it so it didn’t come up to my mind! I’ll try it out if the above solutions do not work!

1 Like

Not to go off-topic but I think you made some pretty good attempts at what you were trying to achieve. Repeat until definitely would have worked, but I just wanted to recommend TweenService because it allows you to show the UI in pretty much any style you might desire, while making your code smaller.

Please keep me posted if any issues arise with my code, and I’ll be glad to help you out :smiley:

1 Like

It’s not much of a problem. Here’s what you need to do.

Step 1: Make a RemoteEvent inside of ReplicatedStorage
Step 2: Type this in the ServerScript

touch.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if player then
        game.ReplicatedStorage:WaitForChild("RemoteEvent_Name_Here"):FireClient(player)
    end
    
    touch:Destroy()
end)

Step 3: Add a LocalScript inside the text label and type this

local TweenService = game:GetService("TweenService")

game.ReplicatedStorage:WaitForChild("RemoteEvent_Name_Here").OnClientEvent:Connect(function()
    local textTween = TweenService:Create(script.Parent --[[Object]], TweenInfo.new(1) --[[Info]], {TextTransparency = 0} --[[Properties to change]])
    local textTween2 = TweenService:Create(script.Parent --[[Object]], TweenInfo.new(1) --[[Info]], {TextTransparency = 1} --[[Properties to change]])
    textTween:Play()
    textTween.Completed:Wait()
    task.wait(1) --Improved version of wait() [Check task library for more info]
    textTween2:Play()
    textTween.Completed:Wait()
end)

Note: I haven’t tested it in studio yet, but I’m really confident about it working.

Extras:

  1. Task Library
  2. TweenService
  3. RemoteEvent
1 Like

No. It’s not a glitch at all. The reason you’re getting this warning is because studio is very against global variables (it’s a really bad practice to use them anyway).
In order to fix your issue, declare a nil player variable just above the if character statement:

	local character = hit.Parent
	
	local player;
	if character then
		player = Players:GetPlayerFromCharacter(character)
	else
		warn("character not found")
	end

You could now also shorten this piece of code to avoid unnecessarily printing 2 warnings every time character fails:

	local character = hit.Parent
	local player = character and Players:GetPlayerFromCharacter(character) or nil; -- if character exists then call the GetPlayerFromCharacter function, otherwise set `player` as nil and terminate the function
	if not player then
				-- you could also print a warning here if you felt the need to
		return;	-- you can now safely remove the next `if player` statement
				-- as `return` will immediately terminate the function
	end

	-- the rest of the code should be here

^ but that’s just as a side note.

Also, as someone suggested below your post it would indeed be a way better idea to use tweenservice instead of 2 while loops.

1 Like