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

Obby "Lava" giving off no error?

Asked by 6 years ago
Edited 6 years ago

I'm making a part in my obby where if you touch the part, it teleports you back to the spawn area instead of killing you. The problem is, I'm getting no error.

local teleportLocation = script.Parent.Parent.TeleportLocation

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player then
        local character = player.Character
        character.HumanoidRootPart.CFrame = CFrame.new(teleportLocation.Position + Vector3.new(0, 2, 0))
    end
end)
0
Ever tried something very useful called "print debugging"? hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This shouldn't be that complicated...

llocal Spawn = game.Workspace:WaitForChild("Spawn")
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
    local character = player.Character
    character.HumanoidRootPart.CFrame = CFrame.new(Spawn.Position)
else end
end)

Please accept my answer if this helped!

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

We would like to use parametersand debounce We use the parameters to access the character And we use debounceso it wouldn't do it too many times(Since doing a normal part.Touchedevent, it would be many times: LeftLeg,RightLeg)

local part = script.Parent
local model = part.Parent.Parent
local spawns = {
    model.Spawns.Spawn1.Position;
    model.Spawns.Spawn2.Position;
    model.Spawns.Spawn3.Position;
    model.Spawns.Spawn4.Position;
    model.Spawns.Spawn5.Position;
    model.Spawns.Spawn6.Position;
    model.Spawns.Spawn7.Position;
    model.Spawns.Spawn8.Position;
    model.Spawns.Spawn9.Position
}
local debounce = false
part.Touched:Connect(function(hit) -- Hit is the `parameter `we are using (The `child `of the `character`)
if hit and hit.Parent and debounce == false then
    debounce = true
    local char = hit.Parent
    local torso = char:FindFirstChild("HumanoidRootPart") -- Finds HumanoidRootPart in the char variable
    for amount,obj in pairs(spawns) do
            math.randomseed(tick())
            local randomSpawn = spawns[math.random(1,amount) ]-- We get the amount in the   table(`spawns:GetChildren()` [<<-- that is stored in a `table`])
            torso.CFrame = CFrame.new(randomSpawn)
        end
        else
            print("hit and hit.Parent is nil") -- Checks if the parameter hit and hit.Parent is nil
    end
    wait()
    if debounce == true then
        debounce = false
    end
end)

As you can see, we have replaced the variable into a table Its more efficient. Plus you were only looking at the index of the spawns:GetChildren() not the object Hopefully this helped

0
It's best to use game.Workspace:WaitForChild("Spawn") instead of using Parent. Also, it be easier to reference the Position of the spawn in the table aswell. PyccknnXakep 1225 — 6y
0
Ok, got it saSlol2436 716 — 6y

Answer this question