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)
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!
We would like to use parameters
and debounce
We use the parameters
to access the character
And we use debounce
so it wouldn't do it too many times(Since doing a normal part.Touched
event, 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