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

How to spawn players on a random part?

Asked by 6 years ago

I'm trying to make a minigame that has a lobby and a map. So if a player presses a ButtonGui when he/she in the lobby he/she will be spawned on a random part (called Spawn) in the map. Can anyone help me make the script?

ButtonGui

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

For this, we'll need to utilize remote events. This assumes you have your spawn parts in a model or folder in workspace named Spawns.

01--Local script
02 
03local button = script.Parent --your textbutton
04local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
05 
06button.MouseButton1Click:Connect(function()
07  remote:FireServer()
08end)
09 
10--Server script
11math.randomseed(tick())
12local remote = Instance.new("RemoteEvent")
13remote.Parent = game.ReplicatedStorage
14local spawns = workspace.Spawns:GetChildren() -- a model of all the spawn bricks
15 
16remote.OnServerEvent:Connect(function(plr)
17   plr:LoadCharacter()
18   local PlayerSpawn = spawns[math.random(1,#spawns)]
19    plr.Character:SetPrimaryPartCFrame(CFrame.new(PlayerSpawn.Position.X,PlayerSpawn.Position.Y + 5,PlayerSpawn.Position.Z))
20end)
0
what's a server script? and where do I put it? backupforibam 0 — 6y
0
Thanks it worked! RealCoolGuysworld 3 — 6y
0
accept answer if it helped Gey4Jesus69 2705 — 6y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

First, make sure the random parts are in a folder called Spawns, that's inside of the map

This is a LocalScript

01local RepStorage = game:GetService("ReplicatedStorage")
02local ClickedEvent = RepStorage:WaitForChild("ClickedEvent")
03local Player = game:GetService("Players").LocalPlayer
04local PlayerGui = Player:WaitForChild("PlayerGui")
05local ButtonGui = PlayerGui:WaitForChild("ButtonGui")
06ButtonGui.MouseButton1Click:Connect(function()
07   local Character = Player.Character or Player.CharacterAdded:wait()
08   if (Character) then
09      ClickedEvent:FireServer(Character)
10   end
11end)

This is a ServerScript

01local RepStorage = game:GetService("ReplicatedStorage")
02local ClickedEvent = RepStorage:WaitForChild("ClickedEvent")
03ClickedEvent.OnServerEvent:Connect(function(Player, Character)
04   local Spawns = workspace:FindFirstChild("MapName").Spawns --// Get Spawns
05   if (Character and Spawns) then --// Precaution
06      local Spawns_ = Spawns:GetChildren()
07      local Spawn = math.random(1,#Spawns) --// Get random Spawn
08      if (Spawn and ButtonGui) then
09         Player:LoadCharacter()
10         Character:MoveTo(Spawns[Spawn]).p  + Vector3.new(0,3,0) --// Teleport
11      end
12   end
13end)

Hope this works

Answer this question