How would I loop a GetDescendants() loop?

@Crrustyy Is this script a Normal Script or a LocalScript?

This script is a server script, the cloning script is a local script

for i,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		v.CastShadow = false
	end
end

function onTouched(hit)
	-- Do Stuff
end

while wait(0.1) do
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Touched:Connect(function(hit)
				if v.Name == "Ending" and v:IsA("BasePart") then
					if hit.Parent:FindFirstChild("Humanoid") then
						onTouched(hit)
					end
				end
			end)
		end
	end
end

How would this remote event take place/look like? I have made one but it doesnt work

He can use WaitForChild(). It will wait until map will appear in workspace.

From my understanding, if the script waits too long, it will say “Infinite yield possible”

I have placed a script (shown below) inside the map, yet this still doesn’t work

function touched(h)
	local part = game.Workspace:WaitForChild("SpawnLocation")
	if h.Parent:FindFirstChild("Humanoid") then
		local humRoot = h.Parent:FindFirstChild("HumanoidRootPart")
		humRoot.CFrame = CFrame.new(part.Position + Vector3.new(0,5,0))
		local plr = game.Players:FindFirstChild(h.Parent.Name)
		game.ReplicatedStorage:FindFirstChild("NewLevel"):FireClient(plr)
		game.ReplicatedStorage:FindFirstChild("Reward"):FireClient(plr)
		h.Parent.Humanoid.WalkSpeed = 16
	else
		return false
	end
end

	script.Parent.Ending.Touched:Connect(function(h) 
		if h then
			touched(h)
		end
	end)

Why your script doesn’t work:

LocalScripts, the server cannot really see action done by these. The action the LocalScript is doing is cloning something. On the server, there is nothing cloned. The script is a Server-Script, so yeah. Your script basically works but it will never work as the server doesn’t know.

Anyway what are you cloning btw?

A map from Replicated Storage.

Why are you using a LocalScript then? Are you trying to make the map only show for the client?

I thought that by using a local script, only that one player can see the clone? That is what I am aiming for.

Correct, but again that’s on the Client… the server doesn’t know that and the script that does something when touched is on the server

How would I make a script, a server script, but only the one player sees the clone?

All actions made by local script can see only player in who local script situated

I think you should clone it from the server, then with a remote event, fire it to the client by doing :FireClient()

1 Like

I am a bit rusty, so could you guide me as to where I should place the remote event?

You can place it everywhere,but most people place it in rep. storage because local and global scripts can access this storage.

So this is my current code in the local script.

Script
local chosenMap = readyMaps[math.random(1,#readyMaps)]
local clonedMap = chosenMap:Clone()
clonedMap.Parent = game.Workspace
local mip = clonedMap.Name
rep.Start:FireServer()

function newLvl()
	clonedMap:Destroy()
	local newMap = readyMaps[math.random(1,#readyMaps)]
	local clone = newMap:Clone()
	clone.Parent = game.Workspace
	local mip = clone.Name
end

rep.NewLevel.OnClientEvent:Connect(newLvl)

How would I change this into a server side script?

Oh, no I knew that.

Sorry, I meant where should I fire the remote event in the script.