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

How can I create a GUI that displays a player's name if they reach a goal?

In my game, players have to race to a "finish line." The teleport script works but I wanted to make this better by creating a GUI that displays for a few seconds and displays the winner's name. Here is my current script for the finish line:

local goal = script.Parent

local function teleport(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do v.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
		end
	end
end
goal.Touched:Connect(teleport)

I've searched many videos on Youtube to try figuring out how to do something like this, but there is no help on there :/ Hopefully you guys have some experience, thanks again :)

1 comment

0

Look into https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireAllClients. Have each player listening to a RemoteEvent. When you fire it, it will prompt a GUI for each player.

1 answer

✓ Accepted answer

There is a lot of ground to cover here. Let me split my answer into two steps.

First create a new LocalScript in the StarterGui section. The important bit is the remote event, that is how clients and server communicate in roblox engine. (There are also remote functions, but we will not use them this time)

--first create a screen gui
local RaceOverScreen = Instance.new("ScreenGui")
RaceOverScreen.Name= "RaceOverScreen"
RaceOverScreen.Parent = script.Parent

--then create a text label in that screen
--adjust visuals to your needs
local ServerBroadcast = Instance.new("TextLabel")
ServerBroadcast.Size = UDim2.new(1,0,0.1,0) --10% of top of the screen
ServerBroadcast.TextScaled = true
ServerBroadcast.Visible = false --only show when necessary
ServerBroadcast.BackgroundTransparency = 1
ServerBroadcast.TextStrokeTransparency = 0
ServerBroadcast.TextStrokeColor3 = Color3.fromRGB(255,255,255)
ServerBroadcast.Parent = RaceOverScreen

--now important part
--crete an client event listener
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaceOverEvent = ReplicatedStorage:WaitForChild("RaceOverEvent") --server will create that for us

local function OnRaceOverEvent(winnerName)
	ServerBroadcast.Text = winnerName .. " has won the race!"
	ServerBroadcast.Visible = true
	wait(3) --how long the message will be visible
	ServerBroadcast.Visible = false
end
RaceOverEvent.OnClientEvent:Connect(OnRaceOverEvent)

Then you need to edit your script, to fire a remote event to the clients. Notice that the server creates the event in the Replicated Storage, and clients just wait for it. This way you can avoid creating duplicate events.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
--create a race over event, it needs to be a remote event
local RaceOverEvent = Instance.new("RemoteEvent") 
RaceOverEvent.Name = "RaceOverEvent"
RaceOverEvent.Parent = ReplicatedStorage

local goal = script.Parent

local function teleport(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then 
        for i, v in pairs(game:GetService("Players"):GetPlayers()) do v.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
        end
		RaceOverEvent:FireAllClients(part.Parent.Name)
    end
	
end
goal.Touched:Connect(teleport)

There are however few problems with your script. First of all you are teleporting player into the spawn instead of above it. Also with many players you should use some kind of offset so they do not spawn in the exact same place. Lastly, if the player just joined and character has not loaded yet, your script will error and will stop working completely.

May I suggest using LoadCharacter instead that will solve all those problems for You? I also added a debounce, so that the event should not fire multiple times.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
--create a race over event, it needs to be a remote event
local RaceOverEvent = Instance.new("RemoteEvent") 
RaceOverEvent.Name = "RaceOverEvent"
RaceOverEvent.Parent = ReplicatedStorage

local goal = script.Parent

justOnce = true
local function teleport(part)
	if justOnce then
	    local humanoid = part.Parent:FindFirstChild("Humanoid")
    	if humanoid then 
		justOnce = false
		local winner = part.Parent.Name -- remeber winner's name
	        for i, v in pairs(game:GetService("Players"):GetPlayers()) do 
				v:LoadCharacter()
	       	end
		delay(1, function()	--make sure players screen has loaded
   			RaceOverEvent:FireAllClients(winner)
		end)
	 	end
		wait(1)
		justOnce = true
	end
end
goal.Touched:Connect(teleport)

Thanks for reading. Have a nice scripting session.

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