Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make it so this spawn script spawns players in the center of the spawn point?

I'm using a free model called "Infinite Obby" which basicly is a script that add's spawn-points with stages. The problem is that when someone spawns it does not spawn them in the center of the spawn point which makes it look bad (In my opinion). I need hep making it so players will spawn in the center of a spawn point like a regular roblox spawn point would.

This is part of the script I think which needs to be edited:

function warpToCheckPoint(torso, cpnum)
	if cpnum == nil then return end
	if torso == nil then return end
	local cp = cpmodel:FindFirstChild(tostring(cpnum))
	if cp ~= nil then
		local chosenCFrame = cp.CFrame	-- Below is the offset
			* CFrame.new(math.random(-cp.Size.X/2,cp.Size.X/2), 2.5, math.random(-cp.Size.Z/2,cp.Size.Z/2))
										-- Above is the offset  ^

		torso.CFrame = chosenCFrame
		wait()
		for k = 1, 10 do
			if torso ~= nil then
				if (torso.Position - cp.Position).magnitude > 3 then
					torso.CFrame = chosenCFrame
				end
				wait()
			end
		end
				
	end
end

This is the whole script:

------------ OPTIONS ------------

local statName = "Stage"			-- You can change this to "Level" or "Chapter" or "Section"
local ModelName = "_CheckPoints"	-- Name of model that stores checkpoint spawnblocks
									-- (You don't HAVE to change this! This is only if you want to.)

local savingEnabled = false		-- Having this set to true will let players save their checkpoint in your game!

local allowDeathReach = false	-- If a player dies but still touches a checkpoint,
								-- It won't count if this is set to false.

local victorySound = true		-- If you want a victory sound for checkpoints, change this to lowercase true.
local victorySoundId = "rbxassetid://12222253"

	---------------------------------




local cpmodel = game.Workspace:FindFirstChild(ModelName)
local cw = game.Workspace:GetChildren()
for iw = 1, #cw do
	if cw[iw].Name == ModelName and cw[iw] ~= cpmodel then
		local copyc = cw[iw]:GetChildren()
		for icopyc = 1, #copyc do
			copyc[icopyc].Parent = cpmodel
		end
		cw[iw]:Destroy()
	end
end				-- ^ These lines of code should help fix in case  you use multiple _CheckPoint models.


local cpmodelc = cpmodel:GetChildren()
for j = 1, #cpmodelc do
	local cpoint = cpmodelc[j]
	cpoint.Touched:connect(function(hit)
		if hit == nil then return end
		if hit.Parent == nil then return end
		if hit.Parent:FindFirstChild("Humanoid") then
			local stop = false
			if hit.Parent:FindFirstChild("Humanoid").Health <= 0 and not allowDeathReach then
				stop = true
			end
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr and not stop then
				if plr:FindFirstChild("CheckPointNumber") then
					local cpnum1 = plr:FindFirstChild("CheckPointNumber").Value
					local cpnum2 = tonumber(cpoint.Name)
					if cpnum1 < cpnum2 then
						plr:FindFirstChild("CheckPointNumber").Value = cpnum2
						if plr:FindFirstChild("leaderstats") then
							local progressdisplay = plr:FindFirstChild("leaderstats"):FindFirstChild(statName)
							if progressdisplay then
								progressdisplay.Value = getDisplayText(cpnum2)
							end
						end
						if victorySound then
							if plr:FindFirstChild("PlayerGui") then
								local sound = Instance.new("Sound")
								sound.Name = "VictorySound"
								sound.SoundId = victorySoundId
								sound.Parent = plr:FindFirstChild("PlayerGui")
								sound:Play()
								game:GetService("Debris"):AddItem(sound, 5)
							end
						end
					end
				end
			end
		end
	end)
end

function getDisplayText(cpnum)
	local displaytext = "[Error!]"	-- If this is showing up, you did not name your checkpoints correctly!
	local cp = cpmodel:FindFirstChild(tostring(cpnum))
	if cp ~= nil then
		local cpname = nil
		local c = cp:GetChildren()
		for i = 1, #c do
			if c[i]:IsA("BoolValue") and c[i].Value == true then
				cpname = c[i]
			end
		end
		if cpname ~= nil then
			if cpname.Name ~= "" then
				displaytext = cpname.Name.." - ["..cpnum.."]"
			else
				displaytext = "["..cpnum.."]"
			end
		else
			displaytext = "["..cpnum.."]"
		end
	end
	return displaytext
end

function warpToCheckPoint(torso, cpnum)
	if cpnum == nil then return end
	if torso == nil then return end
	local cp = cpmodel:FindFirstChild(tostring(cpnum))
	if cp ~= nil then
		local chosenCFrame = cp.CFrame	-- Below is the offset
			* CFrame.new(math.random(-cp.Size.X/2,cp.Size.X/2), 2.5, math.random(-cp.Size.Z/2,cp.Size.Z/2))
										-- Above is the offset  ^

		torso.CFrame = chosenCFrame
		wait()
		for k = 1, 10 do
			if torso ~= nil then
				if (torso.Position - cp.Position).magnitude > 3 then
					torso.CFrame = chosenCFrame
				end
				wait()
			end
		end
				
	end
end

function onPlayerRespawn(property, player)
	if player == nil then return end
	if property == "Character" and player.Character ~= nil then
		local char = player.Character
		local torso = char:FindFirstChild("HumanoidRootPart")
		if torso == nil then
			for i = 1, 10 do
				if torso == nil and char ~= nil then
					torso = char:FindFirstChild("HumanoidRootPart")
					wait(1)
				end
			end
		end
		if torso and player ~= nil then
			local cpnum = player:FindFirstChild("CheckPointNumber")
			if cpnum then
				warpToCheckPoint(torso, cpnum.Value)
			end
		end
		if savingEnabled then
			if player:FindFirstChild("PlayerGui") then
				local save_gui = script.SaveGui:Clone()
				save_gui.Parent = player:FindFirstChild("PlayerGui")
				save_gui.MainFrame.InteractScript.Disabled = false
			end
		end
	end
end

game.Players.PlayerAdded:connect(function(player)
	while player.Character == nil do wait() end
	local char = player.Character
	while char:FindFirstChild("HumanoidRootPart") == nil do wait() end
	local torso = char:FindFirstChild("HumanoidRootPart")

	local cpnum = Instance.new("IntValue")
	cpnum.Name = "CheckPointNumber"
	cpnum.Value = 1
	cpnum.Parent = player

	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local progressdisplay = Instance.new("StringValue")
	progressdisplay.Name = statName
	progressdisplay.Value = getDisplayText(cpnum.Value)
	progressdisplay.Parent = leaderstats

	cpnum.Changed:connect(function()
		progressdisplay.Value = getDisplayText(cpnum.Value)
	end)

	onPlayerRespawn("Character", player)

	player.Changed:connect(function(property)
		onPlayerRespawn(property, player)
	end)

end)

3 comments

0

Please code block everything correctly.

0

I agree w/ DigitalVeer.

0

Sorry first time post, fixed it.

1 answer

✓ Accepted answer

On this line:

local chosenCFrame = cp.CFrame * CFrame.new(math.random(-cp.Size.X/2,cp.Size.X/2), 2.5, math.random(-cp.Size.Z/2,cp.Size.Z/2))

Isn't it the math.random stuff that causes it not to spawn Directly on top of it? Why would you even bother with that if you just want it to spawn right on top? Also, the vertical offset of 2.5 seems a bit low. Since half of the torso's height + the height of the legs is 3, you could try using 3 + cp.Size.Y / 2 to Spawn it directly on top of the Spawn, or 3 + cp.Size.Y / 2 + 0.5 to spawn it a half stud above it, etc. I'd just do this:

function warpToCheckPoint(torso, cpnum)
    if cpnum == nil then return end
    if torso == nil then return end
    local cp = cpmodel:FindFirstChild(tostring(cpnum))
    if cp ~= nil then
        torso.CFrame = cp.CFrame * CFrame.new(0, 3 + cp.Size.Y / 2 + 0.5, 0)
    end
end

2 comments

0

Thanks this worked!

0

@262187 In that case, please select the option to accept my answer. I would appreciate it.

This discussion is preserved as it was. New answers, comments, and votes are closed.