Spawning questions

In my game, I want it so that all players spawn at a certain spawn, and when they click a button, they will be teleported to another spawn. And if lets say ‘x’ teams is chosen, it spawns you at a random spawn point, but all of those spawn points are the same color. (The others aren’t) Thanks!

1 Like

That is a lovely idea you have there! However please remember that the DevForum is for you to receive feedback on your current projects (scripts and models), or get help in the process of making them. You should try attaching screenshots of your current attempt and show us the code you have right now. :slightly_smiling_face:

I just want to know how I would approach this

You could use a ClickDetector for the button which teleports them to their respective spawns. Then the script inside of it would look something like this:

local Detector = script.Parent --Button to click.
local redSpawn = workspace.redSpawnPart --Spawn location.
local greenSpawn = workspace.greenSpawnPart --Spawn location.
local blueSpawn = workspace.blueSpawnPart --Spawn location.

Detector.MouseClick:Connect(function(plr) --First parameter is the instance clicking on the part.
    local Root = plr.Character:WaitForChild("HumanoidRootPart") --Wait for the character to load in case the player clicks the button immediately upon spawning.
    if plr.Team.TeamColor = "Red" then --If red.
        Root.CFrame = redSpawn.Position --Teleport the player.
    elseif plr.Team.TeamColor = "Green" then --If green.
        Root.CFrame = greenSpawn.Position --Teleport the player.
    elseif plr.Team.TeamColor = "Blue" then --If blue.
        Root.CFrame = blueSpawn.Position --Teleport the player.
    end
end)

You should have your spawn location set to “neutral”, so that all players would spawn at the same location by default (if this is what you are trying to achieve). Then you can teleport them from there. If you wanted something else, then just play around with the code! :smiley:

1 Like

How would you tell a script to teleport the player to a random spawn point? Is it like this?

local Tele = {workspace.Spawns:GetChildren}
local ChosenTele = Tele[math.random(1,#Tele)]
script.Parent.MouseButton1Click:Connect(function(plr)
     plr.HumanoidRootPart.CFrame = ChosenTele.CFrame
end

Remember to add the brackets after GetChildren(). Also, the method already returns a table, so no need for the curly brackets:

local Tele = workspace.Spawns:GetChildren()
script.Parent.MouseButton1Click:Connect(function(plr)
    local ChosenTele = Tele[math.random(1,#Tele)]
    plr.HumanoidRootPart.CFrame = ChosenTele.CFrame
end
script.Parent.MouseButton1Click:Connect(function(plr)
     local Tele = workspace.Spawns:GetChildren()
     local ChosenTele = Tele[math.random(1,#Tele)]
     plr.HumanoidRootPart.CFrame = ChosenTele.CFrame
end

run the choosing code when the player presses the button

Umm… what was the point of resending that?

I changed the code to get a random spawn when the player clicked the button not as soon as the game loaded, so everyone wouldn’t spawn in the same place if they clicked that button.

@iHaveAKittySheLoveMe and @jake_4543 It says Players.Kamlkaze_Kid.PlayerGui.ScreenGui.Frame.D_ClassButton.LocalScript:4: attempt to index nil with ‘HumanoidRootPart’ I tried both of your script and this is what the output says

Oh! I did not notice that change haha sorry. Thank you for enlightening me, I’ll edit it.

Oh, sorry. Use plr.Character:WaitForChild("HumanoidRootPart") instead of plr.HumanoidRootPart.

Still says the same thing, I put the code in a serverscript

Could you show a picture of the entire script you are using, and the new output message please?

Never mind, I put the script in the wrong place, now it says: Players.Kamlkaze_Kid.PlayerGui.ScreenGui.Frame.D_ClassButton.Script:4: attempt to index nil with ‘Character’

Again, could you show a picture of the entire script and the output message please? :smiley:

I showed you the output message:

Here is the script:

script.Parent.MouseButton1Click:Connect(function(plr)
     local Tele = workspace.Spawns:GetChildren()
     local ChosenTele = Tele[math.random(1,#Tele)]
     plr.Character:WaitForChild("HumanoidRootPart").CFrame = ChosenTele.CFrame
end)
script.Parent.MouseButton1Click:Connect(function(plr)
    player.CharacterAdded:Connect(function(chr)
        local Tele = workspace.Spawns:GetChildren()
        local ChosenTele = Tele[math.random(1,#Tele)]
        local Root = chr:WaitForChild("HumanoidRootPart")
        Root.CFrame = ChosenTele.CFrame
    end)
end)

This should work!

Roblox made an incredible tutorial that covers your question and it could probably help you later:
https://education.roblox.com/en-us/resources/battle-royale/index

Players.Kamlkaze_Kid.PlayerGui.ScreenGui.Frame.D_ClassButton.Script:2: attempt to index nil with ‘CharacterAdded’
script:

script.Parent.MouseButton1Click:Connect(function(plr)
    plr.CharacterAdded:Connect(function(chr)
        local Tele = workspace.Spawns:GetChildren()
        local ChosenTele = Tele[math.random(1,#Tele)]
        local Root = chr:WaitForChild("HumanoidRootPart")
        Root.CFrame = ChosenTele.CFrame
    end)
end)