How to make a script run when a part is loaded in

How would I make a script in a part run whenever a part is streamed into the workspace?

i.e, utilizing streaming enabled I want a script inside of a light to run on the client whenever the light part is loaded in. I can’t find anything else on this topic.

plz help :smiley:

scripts in parts don’t automatically run when streamed in(or maybe I’m doing something wrong) :frowning:

1 Like

Hey!
Have you tried using game:IsLoaded()?
For example:
You will have to tick on the “Disabled” property of the script first!

while true do
     if game:IsLoaded() then --this will check if the game is loaded or not.
          game.Workspace.Part.Script.Disabled = false --this will enable your script.
          break --stop the loop
    end
    wait(0.1)
end

If you get any errors/ problems, please report it back to me to see if I can help!

1 Like

well problem is the script in the part doesn’t run at all after its streamed in and i’d like to keep the script contained in the part and only have it run locally.

1 Like

This should work

local part = game.workspace:WaitForChild("part")

print("the part is now loaded")

The code below local part will only fire once part is loaded

2 Likes
local run = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer

local part = workspace:WaitForChild("Part")

if workspace.StreamingEnabled then
	run.RenderStepped:Connect(function()
		local distance = player:DistanceFromCharacter(part.Position)
		if distance <= workspace.StreamingMinRadius then
			--Do code.
		end
	end)
end

This is a client-side implementation although I believe it’s what you’re looking for.

and here’s a server-side implementation.

local run = game:GetService("RunService")
local players = game:GetService("Players")

local part = script.Parent

if workspace.StreamingEnabled then
	while true do
		run.Stepped:Wait()
		for _, player in ipairs(players:GetPlayers()) do
			local distance = player:DistanceFromCharacter(part.Position)
			if distance <= workspace.StreamingMinRadius then
				--Do code.
			end
		end
	end
end
3 Likes
repeat wait() until game.Workspace:FindFirstChild("Part")
-- Script
1 Like

it has been 11 months, please do not reply.

The solution was a getchildadded or getdescendantadded connect

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