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

How to make a screenGui with Touched function?

I am making it so when you touch a part it will show a screenGui on your screen (Text for a storyline) but I have no idea how to make it so when you touch the part it shows a screenGui on the player who hit it, I've looked everywhere but couldn't find it, Here is the script if you need it:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

So yeah it is nothing right now but Idk how to make it so after the then It will show a screenGui on hit.humanoid

1 answer

✓ Accepted answer

This can be done with a server script but is pointless as it would just waste server processing when this task can be done locally without the server.

We will need:-

  1. The local players gui

  2. A reference to the part to connect the event

  3. The gui that we want to show (also check for duplicated)

The list above is just a basic example you can add more checks in if needed

This is an example only, there are better solutions:-

local part = -- The location of the part, It is also good to use WaitForChild as we may not have the part ingame. You can also chain link them e.g. game.Workspace:WaitForChild('PartA'):WaitForChild('PartB')

-- We should now have the part so we get the gui that we want to clone
local txtGui = --  again we will need to wait for this gui, I recommend that you put this in the ReplicatedStorage e.g. game:GetService('ReplicatedStorage'):WaitForChild('textGui') ect

--there is a small delay form when the player joins to the player gui being created so we add a wait for child to be safe
local plrGui = game.Players.LocalPlayer:WaitForChild('PlayerGui')

-- we only want to add the gui when the player hits the part not other players
local plrName = game.Players.LocalPlayer.Name

-- now we have all of the required resources we can add the event
-- I would also add a debounce to stop the event from being flooded 
local deb = false
part.Touched:connect(function(hit)
	if deb then return end --  exist the code block if deb is true
	if hit.Parent.Name == plrName  then -- check player name
		deb  = true
		
		-- we now have know the player has hit the part but we want to stop duplicate gui being added
		if not plrGui:FindFirstChild('textGui') then -- if the gui is not found we should add it in
			txtGui:Clone().Parent = plrGui  -- adds the gui to the player
		end

		wait(2)
		deb  = false
	end
end

I have not added a check to see if the player is still alive e.g health > 0. This can be easily added in.

Hope this helps, please comment if you do not understand how / why this code would work.

Useful Links

StarterGui and PlayerGui; Not the Same

Debounce

LocalPlayer

ReplicatedStorage

2 comments

0

Because of the format in your code block can you tell me if I put all of this in one script or if you are just telling me these to use in my own scripts.

0

it is all one script but you will need to add to it to make it work e.g. the parts location.

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