Spawning Models

  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?
    At the moment, it just spawns you at the first one, then you delete and spawns you at the second one. It keeps doing this when I want it to check the spawn point if there is a model there, if so you are spawned at the next one.

    Video:
    https://youtu.be/0816mZVTuSk

  3. What solutions have you tried so far?
    I asked a few people, but they said they don’t know, and I had a look around and I couldn’t find anything that was of use to myself on this forum page.

Normal Script:

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

local TrainSpawnCooldownTime = 60 --// Seconds

local Remotes = ReplicatedStorage.REs --// Folder with remote events
local SpawnTrainRE = Remotes.SpawnTrain
local MenuButtonRE = Remotes.MenuButton

local SpawnPartList = workspace.SpawnParts:GetChildren() --// An array of all spawn points
local Cloneables = ServerStorage.Cloneables
local Train = Cloneables.Train --// The train model

local PlayerInCooldown = {} --// {[Player] = true/nil}

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)

Local Script:

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

script.Parent.MouseButton1Click:Connect(function()
	rE_Spawn:FireServer("Train")
end)
4 Likes

To anyone who may want to help as well, here is what it will do in the future. The model will spawn on these tracks using this part here. So we may need a sensor of some-sort? I wouldn’t know how to script that in though.

image

1 Like

Not sure if I understand or not, but if the problem is, that you want it to check if someone is already spawned at the part, 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

2 Likes

Ah, you understand. Thanks a lot. :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.