I am currently having a problem with figuring out how to remove a value from a table in Roblox Studio. It seems although some code isn’t understanding an update in the table, whilst other code is printing a successful removal in an item from a table.
I have looked into many similar topics, and most of the solutions come down to: “have you tried “Table[Item] = nil”?” Yes. I tried that before I even looked into other dev forum posts relating to my problem. I’ve even talked to Chat GTP many times without a proper solution being given.
Summary of the code (code below): I am making a horror game, and I have a bear (the monster) roam around randomly selected walk points in the map. These walk points are determined by parts I place around the map, and the code grabs a random part from a folder of these parts and gives the bear the position of this randomly selected part (the walk point). The bear will select a walkpoint to move to after finishing walking to a previous walk point. Here’s the problem. If the bear randomly selects a walkpoint it’s already on the exact position of, it will send the bear to Vector3.new(0,-10000000,0). Basically, the bear is sent to nill.
The full lines of code are very large, but the only relevant lines of code are:
(1) The code that includes the table, last target, current target, and the tween that makes the bear move between them.
local AvailableFolder = workspace.BearWalkPoints:GetChildren()
if LastTarget then
print(type(AvailableFolder))
if type(AvailableFolder) == "table" then
print("Table")
-- Remove LastTarget from the AvailableFolder table
for index, walkPoint in ipairs(AvailableFolder) do
if walkPoint == LastTarget then
print("Remove!")
table.remove(AvailableFolder, index)
break -- Exit the loop once the item is removed
end
end
end
end
print(AvailableFolder)
local RandomWalkPoint = AvailableFolder[math.random(1, #AvailableFolder)] --grab a walkpoint
local MovePoint = RandomWalkPoint.Position
local Target = MovePoint -- Target variable for other purposes
if CanClosest == false then
CanClosest = true
elseif CanClosest == true then
CanClosest = false
end
if Running == true and math.random(1,2) == 1 and CanClosest == true then
print("closest player")
Target = BearPosclosestToPlayer(AvailableFolder)
end
if Angermode == true then --and CanClosest == true then
Target = BearPosclosestToPlayer(AvailableFolder)
end
Target = Target + Vector3.new(0,3.5,0)
print(Target)
BearRoot.CFrame = CFrame.lookAt(BearRoot.Position, Target)
LastTarget = RandomWalkPoint
local CanWhileBear = true
local CanTweenPlay = true
local miniRayDistance = -6
local distance = -100
local speed = 9 * (distance/100) -- normally 9 * ...
if Angermode == true then
speed = 4 * (distance/100)
end
local BearSpeed = (Target - BearRoot.Position).Magnitude
local BearWalkTime = BearSpeed * speed / distance
local moveInf = TweenInfo.new(BearWalkTime, Enum.EasingStyle.Linear)
local moveGoal = {Position = Target}
local MoveTS = Tweenservice:Create(BearRoot, moveInf, moveGoal)
MoveTS:Play()
(2) How the closest walk point is determined
local function BearPosclosestToPlayer(Table)
local NearestWalkPoint, NearestPointMagnitude
local PointMagnitude
for i, v in Table do
if NearestWalkPoint then
PointMagnitude = (RootPart.Position - v.Position).Magnitude
if PointMagnitude < NearestPointMagnitude then
NearestWalkPoint = v
NearestPointMagnitude = PointMagnitude
end
else
NearestWalkPoint = v
NearestPointMagnitude = (RootPart.Position - v.Position).Magnitude
end
end
print(NearestWalkPoint.Position)
return NearestWalkPoint.Position
end
How have I tried to fix this? I have tried to fix this by creating a system that holds the walkpoints in a table that the bear will randomly select from. The table will remove the last walk point, so that the bear is not allowed to select the position it is currently standing on when selecting a new walk point. The code seems although it won’t let me remove the last walkpoint from the table; as it will repeatedly act although the last walkpoint is still in the table, even after a print shows it is no longer in the table.
I apologize, as this is quite a length report. Any help is appreciated, thank you!