Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to properly teleport a player when they spawn?

Asked by 9 years ago

I want to exclude the use of a clunky spawn box / touched event respawning system in favor of simply using the CharacterAdded event, but my issue here is that the character doesn't teleport after the event is fired. I've put in a wait(1) as a temporary solution, but it's awkward when you float in space for a moment before being teleported. Using a bindable function here so other scripts can access this function. (On another note, I feel like this should be in server storage so players can't access it.)

local func = Instance.new('BindableFunction')
func.Name = 'TeleportToBase'
func.Parent = Workspace

function func.OnInvoke(char, base)
    base = base or (Workspace:FindFirstChild('Map') or Workspace:FindFirstChild('Spawn')).Base
    local width = base.Size.x/2 - 10
    local length = base.Size.z/2 - 10
    local height = base.Size.y / 2
    char:MoveTo(base.Position + Vector3.new(math.random(-width, width), height + 10, math.random(-length, length)))
end

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(char)
        wait(1)
        func:Invoke(char)
    end)
end)

1 answer

Log in to vote
0
Answered by 9 years ago

I would recommend making a ScreenGui show up each time you spawn which has a black frame around the entire screen, and when you spawn, make it fade out. Either that or decrease the wait time.

Example of making black frame fade out: Make a ScreenGui in StarterGui Name it "Blind" Make a frame that has a black background, size of a scale of 1 on both x and y axis (to make it fit the screen) and make it visible. Keep the name the same (Frame). Replace the code from line 13 onwards with this:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(char)
        wait(1)
        func:Invoke(char)
        for i = 0,1, 0.1 do
            player:WaitForChild("PlayerGui").BlindGui.Frame.BackgroundTransparency = i
        end
    end)
end)
Ad

Answer this question