Stay Alive And Sword Fight | Teleport GUI Help

I need a teleport GUI that teleports you to spawn, so like if you have 500+ time the teleport GUI will show up and you can teleportimage

1 Like

When the player activates the button, you can fire a RemoteEvent to the server that will validate their “time” leaderstats before teleporting their Character to the correct location. This will ensure that the player is not able to modify the leaderstats on the client and teleport themselves to spawn when they do not have enough time:

Example

-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeCheckAndTeleport = ReplicatedStorage.TimeCheckRemoteEvent -- References a RemoteEvent called "TimeCheckRemoteEvent" in the ReplicatedStorage

local button = script.Parent

button.Activated:Connect(function()
    timeCheckAndTeleport:FireServer() -- Activates RemoteEvent when the player clicks the button
end)
-- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeCheckAndTeleport = ReplicatedStorage.TimeCheckRemoteEvent -- References a RemoteEvent called "TimeCheckRemoteEvent" in the ReplicatedStorage

timeCheckAndTeleport.OnServerEvent:Connect(function(player) -- Function is called when RemoteEvent is activated on the server
    local leaderstats = player:FindFirstChild("leaderstats")
    local Time = leaderstats:FindFirstChild("Time")

    if leaderstats and Time then -- If player has leaderstats folder and Time leaderstat, then...
        local timeCheck = Time.Value and Time.Value >= 500
        local Character = player.Character or player.CharacterAdded:Wait()

        if TimeCheck and Character and Character.PrimaryPart then -- If they have 500+ time and their Character & its PrimaryPart exists, then...
            local newCFrame = CFrame.new(0, 100, 0) -- Defines new CFrame value

            Character:SetPrimaryPartCFrame(newCFrame) -- Teleports the player to the newCFrame value
        end
    end
end)

Where do I put the local script and server script?

The scripts can be placed into wherever is best for the current system you have – I structured the LocalScript as being placed directly into the button for this example, as denoted by the usage of script.Parent when referencing the button.

As far as the Server Script goes, that is better suited for the ServerScriptService since it’s only going to be running on the server.

It doesn’t work, I tried it and it didn’t work

Check the Output (which can be enabled from the “View” tab at the top left of the screen).

It’s the greatest tool for quickly determining if something is going wrong. If no errors appear related to the scripts, try adding print statements to determine which parts of the script(s) are running/which conditions are being met.

Take note that if you’re changing your leaderstats on the client to exceed the minimum time value for testing, it won’t work because the server will not see the changes that were made by the client. If that was the case, swap to the server view at the top of the screen to adjust it beforehand.

image

After looking back at my post, I made a slight typo during the original post when checking for “timeCheck” that needs to be corrected:

if TimeCheck and Character and Character.PrimaryPart then

The “TimeCheck” would need to be adjusted to align with the variable that’s called “timeCheck” (since it’s case sensitive).

if timeCheck and Character and Character.PrimaryPart then

Aside from that, if no errors are appearing in the Output while playtesting, try adding print statements as outlined in my previous reply to determine what conditions are/aren’t being met.

Examples

button.Activated:Connect(function()
    print("Button was activated")
    timeCheckAndTeleport:FireServer() -- Activates RemoteEvent when the player clicks the button
end)
timeCheckAndTeleport.OnServerEvent:Connect(function(player)

    print("RemoteEvent was activated by "..player.Name)

    local leaderstats = player:FindFirstChild("leaderstats")
    local Time = leaderstats:FindFirstChild("Time")

    if leaderstats and Time then

        print(player.Name.." has leaderstats folder and time value")

        local timeCheck = Time.Value and Time.Value >= 500
        local Character = player.Character or player.CharacterAdded:Wait()

        if timeCheck and Character and Character.PrimaryPart then

            warn(player.Name.." has enough time to be teleported")

            local newCFrame = CFrame.new(0, 100, 0) -- Defines new CFrame value

so it’s supposed to look like this?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeCheckAndTeleport = ReplicatedStorage.TimeCheckRemoteEvent -- References a RemoteEvent called "TimeCheckRemoteEvent" in the ReplicatedStorage

timeCheckAndTeleport.OnServerEvent:Connect(function(player)

	print("RemoteEvent was activated by "..player.Name)

	local leaderstats = player:FindFirstChild("leaderstats")
	local Time = leaderstats:FindFirstChild("Time")

	if leaderstats and Time then

		print(player.Name.." has leaderstats folder and time value")

		local timeCheck = Time.Value and Time.Value >= 500
		local Character = player.Character or player.CharacterAdded:Wait()

		if timeCheck and Character and Character.PrimaryPart then

			warn(player.Name.." has enough time to be teleported")

			local newCFrame = CFrame.new(0, 100, 0) -- Defines new CFrame value

The only thing that needed to be changed was the timeCheck. The print statements were just examples of where they could be included – if you copy paste the whole codeblock back it won’t work because I omitted the ends of the conditional statements and the function since it was not necessary for the example.

Seems like it didn’t work?

Could you clarify what isn’t working? I don’t see the teleport GUI in the first screenshot and the second screenshot is outside of playtesting.

okay so basically when I enabled the gui in the studio and tested it out, it didn’t teleport me to spawn or when I resetted and had 0 time the teleport gui was still there

Ensure that you’ve changed the CFrame that the Character will be teleported to. I inputted a placeholder value of (0, 100, 0) since I didn’t know what coordinates the spawn is intended to be at in your game.

Remember that you can add print statements to diagnose any problems within the script. I cannot be for sure what would be causing it to work improperly since I’ve tested this in Studio and I haven’t noticed any issues once TimeCheck had been corrected to timeCheck.

Did you implement something that will only display the GUI upon having 500+ time or am I misinterpreting when you’re wanting to have the GUI on the screen? What I provided was only intended to teleport the player upon exceeding the correct value – having the GUI only appear when reaching a certain time was not outlined in the OP.


Example Place

Here’s a barebones example place:

LeaderstatsTeleport.rbxl (25.8 KB)

It works but the overhead is gone

If you mean the BillboardGui that displays their leaderstat value by “overhead”, that is a completely unrelated issue since the code provided does not modify that. The only thing it does after passing the final condition is teleport the Character.