Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I respawn a humanoid once it gets a set amount of distance away from its position?

Asked by 9 years ago

Im very new to scripting and have learned only what is on roblox wiki with no help. I want to set a robot humanoid to respawn if it goes to far from its spawn. I only have a small idea of how this script should be built but I wouldnt even want to type my probable way off base idea of what the script should say. Im sure it would mean asking the workspace what the humanoids position is and at how far in what axis it should move before it respawns. Please help me figure this out.

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Assuming that you already have the spawn part (let's name it SpawnPart) and the robot (let's name it Bot) are all in the Workspace and already set up.

Now, there are two concepts you will need to understand:

  • Regeneration

  • Finding the distance between two objects dynamically

REGENERATION

--Server Script
Bot = workspace.Bot
SpawnPart = workspace.Spawn

BotCLONE = Bot:Clone() -- Cloned Bot to reference the most unaffected model; I'll show you why in a bit.

function RegenBot()
    if Bot then
        Bot:Destroy()
    end
    Bot = BotCLONE:Clone()  -- I cloned the clone. The reason why I is because BotCLONE will basically be the epitome of all Bots, because the original Bot will be deleted when this function is executed.
    local Torso = Bot:FindFirstChild("Torso")
    if Torso then
        Torso.CFrame = SpawnPart.CFrame     -- This will teleport the Bot to the SpawnPart, just in case if the spawn moved to a different position.
    end
    Bot.Parent = workspace
    -- The default Parent of a cloned object is always "nil".
end

DYNAMIC DISTANCE

-- Same Script
Bot = workspace.Bot
SpawnPart = workspace.Spawn

Distance = 20

function CheckBot()
    local Torso = Bot:FindFirstChild("Torso") -- This is needed if you do not like to see errors in your output. Sometimes the function runs, even when Torso is not defined yet due to the clone process.
    if Torso then
        if (SpawnPart.CFrame.p - Torso.CFrame.p).magnitude > Distance then
        -- If the distance between the SpawnPart and the Torso is greater than 20, then the Bot will respawn.
        -- General formula: Final Position - Initial Position (it doesn't really matter since we're dealing with magnitude)
            return true
        else
            return false
        end
    end
end

while wait(2) do    -- I made this check between a long period of time (usually, you'll see the wait time to be 1 second or less), because having the script check the distance between each small amount of time (e.g. 1/30 of a second) is just too much game activity for something so trivial in my opinion.
    if CheckBot() then
        RegenBot()
    end
end

Any questions/comments/skepticism? Ask away.

0
this is exactly what I needed thanks a lot! :) brooksdart 5 — 9y
0
You're welcome. Redbullusa 1580 — 9y
Ad

Answer this question