I have managed to figure out a solution using rays. Apparently, humanoids can make the character jump before it actually ever touches the part. By using a ray that's length is the character's height + the amount the humanoid can hover over a part and still jump it works around this. I found this out from this
Here is my finished code. The 5 can be reduced to something like 4.75 or 4.5 or even down to 3 if it is a tad too big. I found that 3 fit my needs pretty well.
local Players = game.Players
local lava = script.Parent
local spawnObject = lava.Spawn.Value
Players.PlayerAdded:Connect(function(plr)
spawn(function()
while wait(1) do
local char
local runServBind
pcall(function()
char = plr.Character
runServBind = game["Run Service"].Stepped:Connect(function()
local part = workspace:FindPartOnRay(Ray.new(char.HumanoidRootPart.Position,Vector3.new(0,-5,0)),char)
if part then
if part == lava or part:IsDescendantOf(lava) then
char:MoveTo(spawnObject.Position)
end
end
end)
end)
plr.CharacterAdded:Wait()
runServBind:Disconnect()
end
end)
end)
I then added the touched event for the sides to trigger it as well.
lava.Touched:Connect(function(part)
print("Touched")
for i, v in ipairs(Players:GetChildren()) do
pcall(function()
local char = v.Character
if part:IsDescendantOf(char) then
char:MoveTo(spawnObject.Position)
end
end)
end
end)
A few debounces, comments, and fixes later and this was my finishing product:
local Players = game.Players
local lava = script.Parent
local spawnObject = lava.Spawn.Value
local debounce = {checked=false,teleported=false}
function teleportIfTouch(char)
local part = workspace:FindPartOnRay(Ray.new(char.HumanoidRootPart.Position,Vector3.new(0,-3,0)),char)
if part then
if part == lava or part:IsDescendantOf(lava) then
if (debounce.checked == false or (debounce.checked == true and debounce.teleported == false)) and not(debounce.checked == true and debounce.teleported == true) then
debounce.checked = true
debounce.teleported = true
char:MoveTo(spawnObject.Position)
wait(0.1)
debounce.checked = false
debounce.teleported=false
end
end
end
end
Players.PlayerAdded:Connect(function(plr)
while wait(1) do
local char
local runServBind
local runServBind2
local runServBind3
local unspawn = false
pcall(function()
char = plr.Character
spawn(function()
while wait() do
teleportIfTouch(char)
if unspawn then
break
end
end
end)
runServBind = game["Run Service"].Stepped:Connect(function()
teleportIfTouch(char)
end)
runServBind2 = game["Run Service"].Heartbeat:Connect(function()
teleportIfTouch(char)
end)
runServBind3 = game["Run Service"].RenderStepped:Connect(function()
teleportIfTouch(char)
end)
end)
plr.CharacterAdded:Wait()
unspawn = true
runServBind:Disconnect()
runServBind2:Disconnect()
runServBind3:Disconnect()
end
end)
lava.Touched:Connect(function(part)
for i, v in ipairs(Players:GetChildren()) do
local char = v.Character
if part:IsDescendantOf(char) then
if (debounce.checked == false or (debounce.checked==true and debounce.teleported == false)) and not(debounce.checked == true and debounce.teleported == true) then
debounce.teleported = true
debounce.checked = true
char:MoveTo(spawnObject.Position)
wait(0.3)
debounce.checked = false
debounce.teleported = false
end
end
end
end)
#edit: fixed dead link in post
1 comment
A simpler solution than using rays is to have an invisible block set to CanCollide false above the lava and put the touched event on this block instead of the lava.