What do you want to achieve? Keep it simple and clear!
I am trying to spawn the player (the gist of what I’m doing).
What is the issue? Include screenshots / videos if possible!
When I run the script below, sometimes the function fully goes through, but other times it stops at “Function started” (at the top)
game.Players.PlayerAdded:Connect(function(plr)
print("SCRIPT IS RUNNING")
local function tpandspawn ()
print("Function started")
local Players = game:GetService("Players")
print("Player service retrived")
--player needs to be defined here
local player = plr
print(" Going to wait for character to be added")
player.CharacterAdded:Wait()
print("Waiting is over")
local character = player.Character or player.CharacterAdded:Wait() -- Get the character. If it doesn't exist, wait for the event ChildAdded to be called.
wait(1)
print(player.Name .. " joined the game!")
local spawns = { -- side note, the spawns probably don't need this degree of accuracy. You should be able to round off all of those decimals.
CFrame.new(64, 16105, -3289),
CFrame.new(114, 16105, -3289),
CFrame.new(164, 16105, -3289),
CFrame.new(214, 16105, -3289),
CFrame.new(264, 16105, -3289),
CFrame.new(264, 16105, -3289) }
local function spawnPlayer()
local hrp = character:WaitForChild("HumanoidRootPart") -- Get the HumanoidRootPart if it exists, or wait for it to exist. I can't remember if you need to recursively check for things to exist like this, but it won't hurt.
local num = math.random(#spawns)
print(spawns[num])
hrp.CFrame = spawns[num] * CFrame.Angles(0, math.rad(180) , 0)
--Check if the player has actually been teleported
print(hrp.CFrame)
print(character)
print(spawns[1])
print(spawns[2])
print(spawns[3])
print(spawns[4])
print(spawns[5])
print(spawns[6])
table.remove(spawns, num)
print(spawns[1])
print(spawns[2])
print(spawns[3])
print(spawns[4])
print(spawns[5])
print(spawns[6])
local Spawncord = hrp.CFrame
print(spawns[num] * CFrame.Angles(0, math.rad(180) , 0))
if hrp.CFrame == spawns[num] * CFrame.Angles(0, math.rad(180) , 0) then
print("Player should be in position")
else
print("Error Code 565 : Player has not been spawned in position")
--later update: Reset player for fix
end
--change cam
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("Camchange")
local function Camchange ()
-- Fire the remote event
remoteEvent:FireClient(player, Spawncord, num)
end
print("function should connect")
Camchange(Spawncord)
print("serverside connected")
--Anchor head and stop player from moving
local char = player.character
local humanoidroot = char:WaitForChild('HumanoidRootPart')
print("devforum info")
humanoidroot.Anchored = true
print("Your head should be anchored")
local humanoid = char.Humanoid
humanoid.WalkSpeed = 0
end
spawnPlayer()
end
local function GameIntro ()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("Present")
remoteEvent:FireClient(plr)
end
local DSS = game:GetService("DataStoreService")
local store = DSS:GetDataStore("Joins")
local didjoin = false
local firsttime = false
local success, joined = pcall(function()
return store:GetAsync(plr.UserId)
end)
if success then
if joined == 1 then
didjoin = true
print("Welcome back friend!")
tpandspawn()
else
store:SetAsync(plr.UserId, 1)
firsttime = true
print("First time yay!")
tpandspawn()
GameIntro()
end
end
if not success then
print("failed")
end
end)
print("End of script")
print("SCRIPT IS RUNNING")
print("Function started")
local Players = game:GetService("Players")
print("Player service retrived")
--player needs to be defined here
local player = game.Players.LocalPlayer
print(" Going to wait for character to be added")
print("Waiting is over")
local character = player.Character or player.CharacterAdded:Wait() -- Get the character. If it doesn't exist, wait for the event ChildAdded to be called.
print(player.Name .. " joined the game!")
local spawns = { -- side note, the spawns probably don't need this degree of accuracy. You should be able to round off all of those decimals.
CFrame.new(64, 16105, -3289),
CFrame.new(114, 16105, -3289),
CFrame.new(164, 16105, -3289),
CFrame.new(214, 16105, -3289),
CFrame.new(264, 16105, -3289),
CFrame.new(264, 16105, -3289) }
local function spawnPlayer()
local hrp = character:WaitForChild("HumanoidRootPart") -- Get the HumanoidRootPart if it exists, or wait for it to exist. I can't remember if you need to recursively check for things to exist like this, but it won't hurt.
local num = math.random(1,#spawns)
print(spawns[num])
hrp.CFrame = spawns[num] * CFrame.Angles(0, math.rad(180) , 0)
--Check if the player has actually been teleported
print(hrp.CFrame)
print(character)
print(spawns[1])
print(spawns[2])
print(spawns[3])
print(spawns[4])
print(spawns[5])
print(spawns[6])
table.remove(spawns, num)
print(spawns[1])
print(spawns[2])
print(spawns[3])
print(spawns[4])
print(spawns[5])
print(spawns[6])
local Spawncord = hrp.CFrame
print(spawns[num] * CFrame.Angles(0, math.rad(180) , 0))
if hrp.CFrame == spawns[num] * CFrame.Angles(0, math.rad(180) , 0) then
print("Player should be in position")
else
print("Error Code 565 : Player has not been spawned in position")
--later update: Reset player for fix
end
--change cam
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("Camchange")
local function Camchange ()
-- Fire the remote event
remoteEvent:FireServer(player, Spawncord, num)
end
print("function should connect")
Camchange(Spawncord)
print("serverside connected")
--Anchor head and stop player from moving
local char = player.character
local humanoidroot = char:WaitForChild('HumanoidRootPart')
print("devforum info")
humanoidroot.Anchored = true
print("Your head should be anchored")
local humanoid = char.Humanoid
humanoid.WalkSpeed = 0
end
spawnPlayer()
local function GameIntro ()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("Present")
remoteEvent:FireServer(player)
end
I put this into a localscript in StarterCharacterScripts.
well, here’s the thing, this was in a server script so It could spawn each player in an individual box without players getting into each others’. with this model, it only does so for individual clients and it kind of defeats the purpose of the script itself