So I have this code to make trees move. (The code is in StarterCharacterScripts). Every time I run it, it will break and say in the output “green is not a valid member of Model “Workspace.Trees.Tree””, even though all the trees have it inside of it. I used the same exact code in the past in one of my games and had no issue. I don’t know why it’s doing this now. I’m not much of a coder so I am not entirely sure how to fix this. If anyone knows, please help, thank you.
local RNS = game:GetService("RunService")
local PS = game:GetService("Players")
--@ config
local minimumDistance = 170
local rand = Random.new()
local trees = {}
for _,v in ipairs(workspace.Trees:GetChildren()) do
local v2 = v.green
trees[v2] = {
X = 0,
Z = 0,
T = -99999,
Seed = (rand:NextInteger(0, 20) / 10),
Height = v2.Size.Y / 2,
Position = Vector3.new(v2.Position.X, v2.Position.Y - 0.2, v2.Position.Z),
CFRAME = v2.CFrame
}
end
local player = PS.LocalPlayer
RNS.Stepped:Connect(function()
for i,v in pairs(trees) do
if player:DistanceFromCharacter(v.Position) <= minimumDistance then
v.X = v.Position.X + (math.sin(v.T + (v.Position.X / 5)) * math.sin(v.T / 9)) / 3
v.Z = v.Position.Z + (math.sin(v.T + (v.Position.Z / 6)) * math.sin(v.T / 12)) / 4
i.CFrame = CFrame.new(v.X, v.Position.y, v.Z) * CFrame.Angles((v.Z - v.Position.Z)/v.Height, 0,(v.X-v.Position.x)/-v.Height)
v.T += 0.04
else
i.CFrame = v.CFRAME
end
end
end)