Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

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! sne_123456 439 — 3y
0
Hi look at the new edit! sne_123456 439 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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
0
@sne_123456 The issue with that is it cannot get the position information of the trunk InfamousScript 2 — 3y
0
Ok I will edit my answer for the purpose of showing how to get the position of your trunks sne_123456 439 — 3y
0
it makes parts of the tree invisible once the player is at a certain distance to the certain trunk @sne_123456 InfamousScript 2 — 3y
0
ok can you see the edit? sne_123456 439 — 3y
View all comments (16 more)
0
yes it came for an error for me unfortunately. "attempt to index number with number" Line 7 @sne_123456 InfamousScript 2 — 3y
0
ohh I get it sne_123456 439 — 3y
0
Ok now can you try? sne_123456 439 — 3y
0
Sorry i accidentally said trunknum instead of trunk sne_123456 439 — 3y
0
@infamousScript sne_123456 439 — 3y
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 :) InfamousScript 2 — 3y
0
yes try out CFrame sne_123456 439 — 3y
0
what did you get @InfamousScripter sne_123456 439 — 3y
0
If it work please make sure to mark the topic as answered sne_123456 439 — 3y
0
Hello? @InfamousScripter sne_123456 439 — 3y
0
@InfamousScripter the code with position worked for me perfectly, did you miscopy something? sne_123456 439 — 3y
0
I have edited it even further to make only the trunk disappear @InfamousScript and position does work! sne_123456 439 — 3y
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. InfamousScript 2 — 3y
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? sne_123456 439 — 3y
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 InfamousScript 2 — 3y
0
Yes i edited my code to suit you sne_123456 439 — 3y
Ad

Answer this question