Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting script to work with multiple models not just a part from one model?

tree1 = workspace.TREES

print("test")

local tree = tree1:FindFirstChild("treething") -- not sure what to do rather than findfirstchild without it breaking

while true do wait(2) local distance1 = 20 local distance3 = 50 local lowpoly = tree.LowPoly for i, v in pairs(workspace:GetChildren()) do if v:FindFirstChild('Humanoid') then if game.Players:FindFirstChild(v.Name) then if (v.HumanoidRootPart.Position - tree.Trunk.Position).Magnitude > distance1 then print("trunk invisible")

				tree.Trunk.Transparency = 0 -- wanting this to do this to multiple trunks not just one

				--lowpoly.Leaves.Transparency = 1
				--lowpoly.Trunk.Transparency = 1
			end

			if (v.HumanoidRootPart.Position - tree.Trunk.Position).Magnitude > distance3 then
				print("trunk visible")
				tree.Trunk.Transparency = 1

				--lowpoly.Leaves.Transparency = 0.05
				--lowpoly.Trunk.Transparency = 0

			end
		end
	end
end

end

2 comments

0

Hi i have edited the code so that only your trunk is invisible and only your trunk's location is printed, please check the script now!

0

Hi look at the new edit!

1 answer

✓ Accepted answer

I am not a very good scripter, but I think you should use :GetDescendants('trunk') instead of findfirstchild('trunk'). That way you can group all the trees together and just get the trunk.

Heres a sample code i havent tested it and im not 100% sure it will work sorry:

local trunk = game.workspace.trees:GetDescendants('trunk')

trunk.transparency = 1

Now if you want to get the position of your trunk you could use this, this code could help! note i havent tested it!

local trunk = game.workspace.trees:GetDescendants('trunk')
local trunknum = #trunk


for i = 1,trunknum do
	local num = trunk[i]
	if num.Name == 'trunk' then
	  num.Transparency = 1
	  local trunkposition = num.Position -- there might be something to do with vector3 if it cant print out the position i think so you can also use num.CFrame
		print(trunkposition)
  end
end

If you want it for it to change tranparency within a certain distance i have tested this!

local trunk = game.workspace.trees:GetDescendants('trunk')
local trunknum = #trunk
local distance3 = 50

for i = 1,trunknum do
	wait(5)
	local num = trunk[i]
	if num.Name == 'trunk' then
		num.Transparency = 1
		local trunkposition = num.Position -- there might be something to do with vector3 if it cant print out the position i think so you can also use num.CFrame
		print(trunkposition)
		for i, v in pairs(workspace:GetChildren()) do
			if v:FindFirstChild('Humanoid') then
				if (v.HumanoidRootPart.Position - num.Position).Magnitude > distance3 then
					print("its visible now")
					num.Transparency = 0
				else
					print("Not in range")
					num.Transparency = 1
				end
			end
		end
	end
end

20 comments

0

@sne_123456 The issue with that is it cannot get the position information of the trunk

0

Ok I will edit my answer for the purpose of showing how to get the position of your trunks

0

it makes parts of the tree invisible once the player is at a certain distance to the certain trunk @sne_123456

0

ok can you see the edit?

0

yes it came for an error for me unfortunately. "attempt to index number with number" Line 7 @sne_123456

0

ohh I get it

0

Ok now can you try?

0

Sorry i accidentally said trunknum instead of trunk

0

@infamousScript

0

it outputs "Position is not a valid member of Model "Workspace.TREES.treething"" now should I just try out cFrame? thx so much for the help too :)

0

yes try out CFrame

0

what did you get @InfamousScripter

0

If it work please make sure to mark the topic as answered

0

Hello? @InfamousScripter

0

@InfamousScripter the code with position worked for me perfectly, did you miscopy something?

0

I have edited it even further to make only the trunk disappear @InfamousScript and position does work!

0

Yes I got position working I tried texting you on ROBLOX. @sne_123456. It was because it was doing it to the model not the part. Taking this further how can I calculate the players distance so that when within a certain distance it goes visible? Vector3's are annoying as you can't use if statements with them.

0

hi @infamousScript you must have not copid the script correctly it only shows the position of the trunk as it checks for the name 'trunk' did you copy it wrong?

0

@sne_123456 Yes it works for me. But additionally I want to make it so the tree goes visible when you're within a certain range of it

0

Yes i edited my code to suit you

This discussion is preserved as it was. New answers, comments, and votes are closed.