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

How can I make a script where it can find which spawn location it should spawn on?

Keep in mind that I'm v e r y new to scripting in Lua. I'm trying to make an obby, and I want the player to spawn at a certain checkpoint depending on the stage they are on once they fail.

Here's what I thought would work, and I'm uncertain if there's a way to do this.

game.Workspace.Model[leaderstats.Stage.Value]

Basically, I have each checkpoint grouped as a model with their names as "2","3","4", etc...

Pretend I'm on stage 2, and I fail. I have the player teleported to the stage's checkpoint (which is determined by the name of the Checkpoint in their own groups/models).

How can I achieve that?

Full Script (Located in ServerScriptStorage):

function repsawn(object)
	local player = game.Players:GetPlayerFromCharacter(object)
	if player ~= nil then
		local ls = player.leaderstats
		local checkpoint = game.Workspace.Model[ls.Stage.Value]
		object.HumanoidRootPart.CFrame = object.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
		wait()
		object.HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,3,0)
	end 
end

game.Workspace.ChildAdded:connect(repsawn)

If I missed any info probably did, then please tell me! :D

1 answer

Problem:

  • You used object.HumanoidRootPart.

Solution:

  • Use object.Parent.HumanoidRootPart as the object that hit was a body part, (Right Leg, Left Leg, etc) not the actual model.

Recommendations:

  • Use connect with an uppercase "C" as connect with a lowercase "C" is deprecated. It is never a good idea to use deprecated functions.

  • Put "local" before defining a function as it makes it local like a local variable.


Fixed Script:

local function repsawn(object)
    local player = game.Players:GetPlayerFromCharacter(object)
    if player ~= nil then
        local ls = player.leaderstats
        local checkpoint = game.Workspace.Model[ls.Stage.Value]
        object.Parent.HumanoidRootPart.CFrame = object.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
        wait()
        object.Parent.HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,3,0)
    end 
end

game.Workspace.ChildAdded:Connect(repsawn)

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