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

How Do I Get Players To Spawn On A Parts Position At Random inside of a Model?

Asked by 5 years ago

Here Is My Attempt, I tried Several Times Cannot Find a Way

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameInProgress = ReplicatedStorage:WaitForChild("GameInProgress")

local Lobby = workspace.Lobby



game.Players.PlayerAdded:Connect(function(Player)

if GameInProgress == true then

Player.Character.HumanoidRootPart.CFrame = CFrame.new()

end

end)
0
One thing wrong is that `GameInProgess` is an object, not a boolean, theking48989987 2147 — 5y
0
Yeah I Know about that Thanks Thoe RobloxScriptingA 0 — 5y
0
Get the position of the parts and use math.random Prince_Boujeee 0 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I would do something along the lines of defining the spawns model first

local Spawns = yourSpawnsModelPath:GetChildren();

then I would create a function to get the randomspawn

function getRandomSpawn()
    local ranNum = math.random(1,#Spawns);
    local Spawn = Spawns[ranNum];
    return Spawn;
end

Then I will create a function to teleport the player to the random spawn

function teleportPlayerToSpawn(player)
--Check to see if the player exist
    if game.Players:FindFirstChild(player) then
    --defining the character 
        local char = plr.Character or plr.CharacterAdded:Wait()
        --grabbing the part you want to teleport usually the hrp
        local hrp = char:WaitForChild("HumanoidRootPart")
       -- making sure it exist
        if hrp then
        -- making sure the randomspawn exist
            if getRandomSpawn() then
               -- changing the players position to the random spawn
                hrp.CFrame = getRandomSpawn().CFrame * CFrame.new(0,3,0)
            end
        end
    end
end

Then call the function when needed teleportPlayerToSpawn(player) dont forget the player variable pass through the player object.

I am not 100% sure this work neither if it's the best method made it off the spot hope it works! Upvote if it helps! -cc

0
i wouldn't recommend using Position as it takes into account collision theking48989987 2147 — 5y
Ad

Answer this question