Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

3 comments

0

One thing wrong is that GameInProgess is an object, not a boolean,

0

Yeah I Know about that Thanks Thoe

0

Get the position of the parts and use math.random

1 answer

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

1 comment

0

i wouldn't recommend using Position as it takes into account collision

This discussion is preserved as it was. New answers, comments, and votes are closed.