Monster underground transportation script gone wrong

I’m trying to make an enemy for my tower defense game that can go underground and teleport farther into the map to escape towers. However, the teleportation doesn’t work for some reason.
Script that makes the monster go underground and (not yet) teleport:

repeat
	task.wait()
until (script.Parent):IsA("Model")
if script.Parent.Name == "Umbra Monstrum" then
	local hrp = script.Parent:WaitForChild("HumanoidRootPart")
	local ts = game:GetService("TweenService")
	local pathystuff = (game.Workspace:WaitForChild("PathWay"))
	local tweeninfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local hum = script.Parent:WaitForChild("Humanoid")
	local isunderground = hum.Parent:WaitForChild("IsUnderground")
	local path = script.Parent:WaitForChild("ChosenPath")
	local currentyeeyee = hum.Parent:WaitForChild("Ajajajajaja")
	local pathy = pathystuff:WaitForChild(path.Value)
	local merge = pathystuff:WaitForChild("Merge")
	local tableofstuff = {}
	for i, v in pairs(pathy:GetChildren()) do
		table.insert(tableofstuff, v)
	end
	for i, v in pairs(merge:GetChildren()) do
		table.insert(tableofstuff, v)
	end
	isunderground.Value = false
	hum.HealthChanged:Connect(function(health)
		local max = hum.MaxHealth
		if health*2 == max then
			hum.WalkSpeed = 0
			local animcontroller = hum:WaitForChild("Animator")
			local anim = hum:WaitForChild("Shadow")
			local shadowanimation = animcontroller:LoadAnimation(anim)
			shadowanimation.Priority = Enum.AnimationPriority.Action3
			shadowanimation:Play()
			task.wait(2)
			local tween = ts:Create(hrp, tweeninfo, {CFrame = hrp.CFrame * CFrame.new(0, -8, 0)})
			hrp.Anchored = true
			tween:Play()
			print("yee yee")
			tween.Completed:Wait()
			isunderground.Value = true
			hrp.Anchored = false
			print(pathystuff)
			print(currentyeeyee.Value) --Prints 4
			local findyfind = table.find(tableofstuff, tostring(currentyeeyee.Value))
			print(findyfind) --Prints nil (for some reason), there is only from 1-4
			
		end
	end)
	task.wait(5)
	hum.Health = 3 --Test purposes only
end

There’s a script that changes Ajajajajaja (pathfinding script):

repeat
	task.wait()
until (script.Parent):IsA("Model")
local col = require(game.ServerStorage:WaitForChild("Collisions"))
local inunderground
local hum = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
if script.Parent.Name == "Umbra Monstrum" then
inunderground = script.Parent:WaitForChild("IsUnderground")
end
local CODINGISHARD
local currentyeeyee = hum.Parent:WaitForChild("Ajajajajaja")
local path = script.Parent:WaitForChild("ChosenPath")
local pathway = game.Workspace:WaitForChild("PathWay")
local chosenPath = pathway:WaitForChild(path.Value)
local merged = pathway:WaitForChild("Merge")
local chosenchildren = chosenPath:GetChildren()
local mergedchildren = merged:GetChildren()
local pathpoints = {}
local undergroundpath = (game.Workspace:WaitForChild("UndergroundPathWay")):GetChildren()
col.SetPartToGroup(undergroundpath, "Paths and Doorways")
for i, v in pairs(chosenchildren) do
	table.insert(pathpoints, v)
end
for i, v in pairs(mergedchildren) do
	table.insert(pathpoints, v)
end
CODINGISHARD = 0
for i, v in pairs(pathpoints) do
	if i == 5 then
		break
	end
	CODINGISHARD = i
	currentyeeyee.Value = i
	print(currentyeeyee.Value)
	inunderground.Changed:Connect(function(val)
		CODINGISHARD = i+1
		if CODINGISHARD == 5 then
			CODINGISHARD = 4
		end
	end)
	if i~=CODINGISHARD then
		continue
	end
	hum:MoveTo(v.Position, v)
	hum.MoveToFinished:Wait()
	if (hrp.Position - v.Position).Magnitude > 1 and (hrp.Position - v.Position).Magnitude < (hrp.Position - (pathpoints[i]).Position).Magnitude then
		hum:MoveTo(v.Position, v)
		hum.MoveToFinished:Wait()
	end
end

Thanks so much :slight_smile:

What is currentyeeyee cause tableofstuff is a table of instances and your looking for a string in it so might be the problem.

currentyeeyee (aka Ajajajajaja) is a string value, how do I make it look for an instance with currentyeeyee’s value as its name?

from what I understand this could work.

local findyfind

for _, v in pairs(tableofstuff) do
    if v.Name == tostring(currentyeeyee.Value) then
        findyfind = v
        break
    end
end
1 Like

Thanks, I’ve moved on to the actual teleportation:

			local findyfind
			for _, v in pairs(tableofstuff) do
				if v.Name == currentyeeyee.Value then
					findyfind = v
					break
				end
			end
			hrp.CFrame = findyfind.CFrame * CFrame.new(0, -8, 0) --Line 49

However, it errors:

Workspace.Umbra Monstrum.Umbra Monstrum GoUnderground:49: attempt to index nil with 'CFrame'  -  Server - Umbra Monstrum GoUnderground:49

Any idea on how to fix this?

what value is nil hrp or findyfind?

For some reason findyfind is still nil, even though there is an instance named 4 in the table

Make sure the instance names and currentyeeyee.Value dont have any spaces.

for _, v in pairs(pathy:GetChildren()) do
    tableofstuff[v.Name] = v
end
for _, v in pairs(merge:GetChildren()) do
    tableofstuff[v.Name] = v
end

local findyfind = tableofstuff[tostring(currentyeeyee.Value)]

print(findyfind)

if findyfind then
    -- code here
end

you can try this.

Though your solution wasn’t exact:

findyfind = tableofstuff[currentyeeyee.Value]

I still think that can be counted as a solution.
Thanks for your help :slight_smile:
(Now I just need to figure out how to not teleport it directly to the end…)

1 Like

thats great have fun trying to figure that out.

1 Like