How could I wait for a tool to fully load, to then make it all transparent?

currently I have a script that’ll pick a random weapon, clone it, and put it inside the player’s character. I also have a viewmodel script working along side this

everything works perfectly fine! until I load the player character… the way my viewmodel script works is it checks the player for a tool being added, and then gets the tool’s children, and making it transparent. this doesn’t work when the player is loaded. the tool is cloned and forced into the character, but it hasn’t fully loaded on the client, thus it does not change any of the parts.

so in full I just need to know how I could wait for the tool to fully load before I make it transparent for the localplr, or maybe I should be doing this differently

1 Like

What you could do on the client’s side is

if not game:IsLoaded() then
      game.Loaded:Wait()
end

Course this would only work in certain cases, depending on where your script is located and where the tools are located.
If that doesn’t work, you could try looping through it’s original parent of the tool and use :WaitForChild().

1 Like

I thought of doing this but I feel like it wouldn’t be the best way of doing it. I might use it in the end though

I thought of this too but when I’m firing Plr:LoadCharacter() on the server the game is most likely already loaded for the player

either way I think the :WaitForChild() is the best I’ll get

now that I remember, the original tool is held in serverstorage. so the client wouldn’t be able to acquire it

would it be better to keep the tools in replicatedstorage? the only reason they’re in serverstorage is cause I had concerns for exploiters

You could use the ChildAdded event to detect when they’re done loading:

local tool = script.Parent


local function onChildAdded(instance: Instance)
	if instance:IsA("BasePart") then
		instance.Transparency = 1
	end
end

tool.ChildAdded:Connect(onChildAdded)

for _, instance in tool:GetChildren() do onChildAdded(instance) end

You could also use DescendantAdded if you’d like to detect parts that are children of other parts which are inside of your Tool:

local tool = script.Parent


local function onDescendantAdded(instance: Instance)
	if instance:IsA("BasePart") then
		instance.Transparency = 1
	end
end

tool.DescendantAdded:Connect(onDescendantAdded)

for _, instance in tool:GetDescendants() do onDescendantAdded(instance) end

Don’t forget to disconnect the connection once it’s no longer required, though :slight_smile::+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.