How do you wait until a part exists?

I made a gun that ejects shells in the server, and I want the local player to not be able to see the ejected shells in the server. I already made a script, but the problem is that it breaks because it doesn’t detect the ejected shell in workspace.

SCRIPT:
local part = workspace:WaitForChild(“BulletShell”)

local function modify(part)
part.Transparency = 1
end

while true do
wait()
modify(part)
end

2 Likes

If you are creating the part by a script and want a local script to wait until the part exists, then you should make the script fire a remote event and have the local script do RemoteEvent.OnClientEvent:Wait(). Nothing below that will run until the remote event is fired.

If this is a server script that’s waiting for a part which was created by the server, use a bindable event and any code under BindableEvent.Event:Wait() will not run until the event is fired.

I’m not sure what the two arguments should be though. This is my server script that creates the part:

	local mag = game.ServerStorage.P2000_mag:Clone()
	mag.CFrame = CFrame.new(script.Parent.Parent.Parent.Parts.Mag.Position)
	mag.Anchored = false
	mag.CanCollide = true
	mag.Orientation = Vector3.new(script.Parent.Parent.Parent.Parts.Mag.Orientation.X, script.Parent.Parent.Parent.Parts.Mag.Orientation.Y, script.Parent.Parent.Parent.Parts.Mag.Orientation.Z)
	mag.Parent = game.Workspace
	mag.Massless = false
	mag.Transparency = 0
	game:GetService("Debris"):AddItem(mag,5)
end)```

Is the script that you want to detect this on a local script or a server script?

It’s on a local script (30 characters))))

I’d personally use

repeat wait() until workspace.partname
1 Like

If you didn’t know, to fire to a RemoteEvent to a client requires you to pass the argument of the player aka. client you want it to receive the event as the first parameter.

For getting the part in the localscript you should just send the part with the event.

The part is created by the server. The problem is that the LocalScript cannot detect the part on the client
so your example won’t work.

Can it not? welp the more ya know

Cool, so what you should do is create a RemoteEvent. Once the part is created on the server, have the server fire the event with RemoteEvent:FireClient(player)
Then, in the local script which detects the part, use RemoveEvent.OnClientEvent:Wait(). This will wait until the event is fired, so nothing under that line will run until the event is fired, AKA until the part is created

Yes I get what you’re saying but I’m not sure how I’m supposed to get the player through this server script that creates the part

script.Parent.OnServerEvent:Connect(function()

local mag = game.ServerStorage.P2000_mag:Clone()

mag.CFrame = CFrame.new(script.Parent.Parent.Parent.Parts.Mag.Position)

mag.Anchored = false

mag.CanCollide = true

mag.Orientation = Vector3.new(script.Parent.Parent.Parent.Parts.Mag.Orientation.X, script.Parent.Parent.Parent.Parts.Mag.Orientation.Y, script.Parent.Parent.Parent.Parts.Mag.Orientation.Z)

mag.Parent = game.Workspace

mag.Massless = false

mag.Transparency = 0

game:GetService(“Debris”):AddItem(mag,5)

end)

Nvm guys I figured it out. It didn’t have anything to do with remotes or that stuff. It was actually pretty simple. I just destroyed it instead of changed the transparency to 1

while true do
wait()
for i,v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "P2000_mag" then
		v:Destroy()
	end
	end
	end```