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)
local RaceOverScreen = Instance.new("ScreenGui")
RaceOverScreen.Name= "RaceOverScreen"
RaceOverScreen.Parent = script.Parent
local ServerBroadcast = Instance.new("TextLabel")
ServerBroadcast.Size = UDim2.new(1,0,0.1,0)
ServerBroadcast.TextScaled = true
ServerBroadcast.Visible = false
ServerBroadcast.BackgroundTransparency = 1
ServerBroadcast.TextStrokeTransparency = 0
ServerBroadcast.TextStrokeColor3 = Color3.fromRGB(255,255,255)
ServerBroadcast.Parent = RaceOverScreen
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaceOverEvent = ReplicatedStorage:WaitForChild("RaceOverEvent")
local function OnRaceOverEvent(winnerName)
ServerBroadcast.Text = winnerName .. " has won the race!"
ServerBroadcast.Visible = true
wait(3)
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")
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")
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
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
v:LoadCharacter()
end
delay(1, function()
RaceOverEvent:FireAllClients(winner)
end)
end
wait(1)
justOnce = true
end
end
goal.Touched:Connect(teleport)
Thanks for reading. Have a nice scripting session.
1 comment
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.