#Character flungs into sky after respawn-reset
1 messages · Page 1 of 1 (latest)
```lua
local function teleportPlayersToMap(map)
local spawnPoints = map:FindFirstChild("SpawnPoints")
if not spawnPoints or #spawnPoints:GetChildren() == 0 then
return
end
local spawnPointsList = spawnPoints:GetChildren()
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(function()
local character = player.Character
if not character then
local startTime = tick()
repeat
task.wait(0.1)
character = player.Character
until character or (tick() - startTime) > 5
if not character then
return
end
end
local humanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)
if not humanoidRootPart then
return
end
local randomSpawn = spawnPointsList[math.random(1, #spawnPointsList)]
character:PivotTo(randomSpawn.CFrame * CFrame.new(0, 2, 0))
end)
end
task.wait(1)
end```
.
What is this script for?
Why do you need to tp players?
Also when do you call this function?
Also for position you should use vector3 Imo
randomSpawn.CFrame * CFrame.new(0, 2, 0) is probably not going to have the results you wanted
Was about to say that lol
I am teleporting all players to random position when a specific button is clicked
I'd recommend using vector3 or just random spawn.CFrame
Ur first issue is probably not to do with this script
Or if it is it's with the pivot to
record this 'flinging' and 'teleporting into the sky' maybe?
I found it guys! It was this script:
```lua
local playSoundForLocalPlayer = game:GetService("ReplicatedStorage"):WaitForChild("PlaySoundForLocalPlayer")
local players = game:GetService("Players")
local debounce = {}
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
if character and character:FindFirstChild("Humanoid") then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local player = players:GetPlayerFromCharacter(character)
if humanoidRootPart and player and not debounce[player] then
local isPlayerInMap = character:FindFirstChild("PlayerInMap")
if isPlayerInMap then
if isPlayerInMap.Value == true then
isPlayerInMap.Value = false
end
end
debounce[player] = true
-- Teleport player
humanoidRootPart.Position = game:GetService("Workspace"):WaitForChild("LobbyTeleporter").Position + Vector3.new(0, 1, 0)
playSoundForLocalPlayer:FireClient(player, script:WaitForChild("SplashWaterEffectSound"))
task.delay(2, function()
debounce[player] = false
end)
end
end
end)
** You are now Level 7! **
yep that'd do it
It is better using character:PivotTo instead of humanoidRootPart i guess
wat no it's because you're setting the position of the part instead of cframe
directly setting part.position unwelds/unconstrains the part during the move, so offsets change. you're left with a character whose root part can be very far away from the rest of the actual character
ohhhh
Yeah pivot to or cframe
Usually
ya if you want to move the whole assembly always set cframe not position
doesn't matter if you pivotto or set the part cframe, the whole assembly moves as one model in both cases
In my case it is better to use pivotTo cause i am teleporting multiple players and it reduces lag
Thanks a lot for offering help guys!
'it reduces lag'
i'm just ... going to pretend i didn't hear that.
How would it reduce lag?
it's better not to ask
Using CFrame teleports all parts of the character, no? And if i use PivotTo, it teleports the whole model making it struggle less
k never mind
i will say this though, for the moment, do not prioritize optimization
nothing in your code could possibly be so bad that it drops framerate below 60fps, so you're fine.
So, is there a more efficient way of teleporting like 10 players at once?
technically yes but dont worry about it
Alright
It's better to optimize now than later but yeah don't need to optimize for 100% especially not when you just started making a game
9 times out of 10 these micro 'optimizations' are extremely premature, or make things more difficult than they ever need to be
ehhhhhhhhhhhh optimization is a final step not a first one
Yeah and the funny part is that sometimes it makes the performance worse 🥀
exactly 👌
When should you optimize your code?
Access to code examples, deleted scenes, song names and more at https://www.patreon.com/codeaesthetic
I mean you should optimize spawning 1000s of bullets for a minigun game or smth
ya this video covers most of the issues with premature optimization nicely including this.
I am actually optimizing my game right now, so i am closer to publishing it than a new start
start with some working prototype - and if there is a real performance problem, measure, try something, measure again.
don't be trying to fix performance problems that dont exist yet
it's all in the video so 👍
Yeah that's real
okay