Model Spawning Assistance

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I would like to achieve when you press the spawn button, you get spawned in at one of the green parts (these are our spawn points). However, right now it spawns you at one, then you delete it and it spawns you at the other. Is there a way to make it so it checks a certain area before spawning to see if there is a model there, and if so it spawns you at the other part?

  2. What is the issue?
    Someone has told me to how to do it, but I don’t know how to write it in this script.

  3. What solutions have you tried so far?
    I asked someone else but they said they didnt understand.

  4. What they said?
    All you need to do is assign an Attribute to the part, and either give it a boolean, saying someone is spawned there already, or give it the players UserId number.
    Always check the Attribute before spawning a character to the part, only spawn them if the attribute is nil or false

Server

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TrainSpawnCooldownTime = 60

local Remotes = ReplicatedStorage.REs
local SpawnTrainRE = Remotes.SpawnTrain
local MenuButtonRE = Remotes.MenuButton

local SpawnPartList = workspace.SpawnParts:GetChildren()
local Cloneables = ServerStorage.Cloneables
local Train = Cloneables.Train

local PlayerInCooldown = {}

local CurrentSpawnIndex = 1

local function CloneTrain(player)
	if not player.Character then
		return;
	end

	local Humanoid = player.Character:FindFirstChild("Humanoid")
	local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
	if not Humanoid or not HumanoidRootPart then
		return;
	end

	if PlayerInCooldown[player] then
		return;
	else
		PlayerInCooldown[player] = true
	end

	local SpawnPart = SpawnPartList[CurrentSpawnIndex]

	local NewTrain = Train:Clone()
	NewTrain.Name = player.Name .. "'s Train"
	NewTrain.Parent = workspace
	NewTrain:PivotTo(SpawnPart.CFrame)

	local PreviousSeat = Humanoid.SeatPart
	if PreviousSeat then
		Humanoid.Sit = false
		player.Character:SetAttribute("IsSitting", false)
	end

	local VehicleSeat = NewTrain.PrimaryPart:FindFirstChild("VehicleSeat")
	if VehicleSeat then
		HumanoidRootPart.CFrame = VehicleSeat.CFrame
		VehicleSeat:Sit(Humanoid)

		player.Character:SetAttribute("IsSitting", true)

		Humanoid.JumpHeight = 0
	end

	CurrentSpawnIndex += 1
	if CurrentSpawnIndex > #SpawnPartList then
		CurrentSpawnIndex = 1
	end

	task.wait(TrainSpawnCooldownTime)
	PlayerInCooldown[player] = nil
end

local function DeleteTrainAndResetCooldown(player)
	print("Ran")
	local Train = workspace:FindFirstChild(player.Name .. "'s Train")
	
	print("Train is " .. tostring(Train))
	if Train then
		Train:Destroy()

		PlayerInCooldown[player] = nil
	end
end

SpawnTrainRE.OnServerEvent:Connect(CloneTrain)
MenuButtonRE.OnServerEvent:Connect(DeleteTrainAndResetCooldown)

Client

local RS = game:GetService("ReplicatedStorage")
local REs = RS.REs
local rE_Spawn = REs.SpawnTrain

script.Parent.MouseButton1Click:Connect(function()
	rE_Spawn:FireServer("Train")
end)
1 Like

You need to get a random index.
This can be acquired by using math.random(1, #table)

So it should look something like this:

local randomIndex = math.random(1, #SpawnPartList)

local SpawnPart = SpawnPartList(randomIndex)

We have an index if that’s what youre reffering to.

image


This is purely what I’m asking to implement, I don’t know how.

Oh, I thought that you wanted to make the spawns random. Apologies.

You can create zones for each spawn and use workspace:GetPartsInPart()
to check for characters inside it.

Some games have such systems. In that case you would need to check in what room the active players are. You can use the Part:GetTouchingParts() to see if a player is in the room. If there is no player present in that room you are able to spawn. As to doing that just use a foreach loop to quickly move threw all rooms.

Would this be simpler than using a boolean?

Well that’s just a different function used. However he didn’t mention the full logic of how the algorithm should work

All I really wanted was to know how to implement the boolean, e.g if someone could implement it as I’m confused. (in my script)

You could do something like:

local overlapParams = OverlapParams.new()
overlapParams.BruteForceAllSlow = true
local touchingParts = workspace:GetPartsInPart(SpawnPart)

local isSafe = true
for _, part in pairs(touchingParts) do
 if part.Parent and part.Parent:FindFirstChild("Humanoid") then
  isSafe = false
 end
end
if isSafe then
 -- rest of the code
end

Sorry for the late response I’ve been busy since school started.

All good… we sorted it ourself however, thanks for the assistance. :slight_smile:

1 Like