-
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? -
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 -
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)