You can write your topic however you want, but you need to answer these questions:
-
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?
Someone has told me to how to do it, but I don’t know how to write it in this script. -
What solutions have you tried so far?
I asked someone else but they said they didnt understand. -
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)