Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this script I made occasionally work?

##Problems

In the ServerScript, there is a part named ExitRegion listening to the Touched event. When there are 2 or more players and they touch, they occasionally teleport. It would freeze the screen for a second if it worked. It wouldn't And to actually get out(the last iteration was supposed to do that), you have to reset.The while loop that checks if the length of the inRound array is 0, it just breaks and continues on(and doesn't teleport). Plus when touching the ExitRegion, there is a :FireClient() method. It works with GUI's. It does work but the recurse() function I have. It doesn't disable the scripts found. There are no errors in the output.

##Code

ServerScript in ServerScriptService

--\\ Services
local repStorage = game:GetService("ReplicatedStorage")
--\\ Network Data
local exitRemote = repStorage:WaitForChild("ExitRemoteEvent")
local resetLighting = repStorage:WaitForChild("LightingRemoteEvent")

local inRound = {}
local finished = {}

local participating = game.Players:GetPlayers()

for index,plr in pairs(participating) do
	print("Inserted a player in the inRound array")
	inRound[#inRound + 1] = plr.Name
	local foundPlayer = players:WaitForChild(plr.Name)		
	local char = game.Workspace:WaitForChild(foundPlayer.Name) or foundPlayer.Character
	local hum = char:WaitForChild("Humanoid")
				
	local function removePlayer()
		inRound[index] = nil
		print("Now #inRound has decreased to " .. #inRound)
		resetLighting:FireClient(foundPlayer,newMap)
		print("Reseting lighting for " .. foundPlayer.Name)
	end
				
	--\\ Removing Connections
	hum.Died:Connect(removePlayer)	 
	players.PlayerRemoving:Connect(function(leavingPlayer)
		for i = 1,#inRound do
			if inRound[i] == leavingPlayer then
				print("Removing " .. leavingPlayer.Name)							
				removePlayer()
			end
		end
	end)
end
--\\ Exit Player
local exitRegion = game.Workspace:WaitForChild("ExitRegion")
local db = false
local function exit(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not db then
		db = true
		local player = players:GetPlayerFromCharacter(hit.Parent)	
		--\\ Finish Player
		if player then
			print(player.Name .. " isn't nil")
			for index,finisher in pairs(inRound) do
				if finisher == player.Name and not finished[index] then 
					finished[#finished + 1] = finisher
					print(player.Name)
					exitRemote:FireClient(player,exitRegion)
					inRound[index] = nil
					break
				else
					print(finisher .. " is not " .. player.Name)
				end 
			end	
		end
		wait(1)
		db = false
	end
end
exitRegion.Touched:Connect(exit)

while #inRound > 0 do
	wait(1)
	currentTime = currentTime + 1
	if currentTime == MAX_TIME then
		print("Max time reached")
		for i = 1,#inRound do inRound[i] = nil end
		print("currentTime variable has resetted to " .. currentTime)
		break
	end
	if #inRound <= 0 then
		break
	end
end

--\\ Teleport player back
for _,winner in pairs(finished) do
	print("Looping finished players")
	local char = game.Workspace:FindFirstChild(winner)
	if char:FindFirstChild("HumanoidRootPart") then
		debugPrint("Character's HumanoidRootPart is not nil!")
		local torso = char:WaitForChild("HumanoidRootPart")
		torso.CFrame = CFrame.new(lobbySpawn.Position)
		print(winner .. " has been inserted in the finished array")
	else
		print("The Character's HumanoidRootPart is nil!")
	end
end

A recursive function in a LocalScript(this is with exitRemote.OnClientEvent event) in StarterGui

local function recurse(model)
	for _,child in pairs(model:GetChildren()) do
		if child:IsA("Model") then
			recurse(model)
			print("Child is a model, fixing!")
		elseif child.Name == "KillScript" and child:IsA("Script") then
			child.Disabled = true
		end
	end
end
	
recurse(game.Workspace:WaitForChild("Map"))

5 comments

0

you activated the output tab?

0

Why are you calling the function within the function?

0

I am calling a function within a function(recursion) because I need to find the model's children. I don't know when it ends so I simply use recursion

0

You can use GetDescendants now instead of GetChildren to get all descendants of the model (including children of children of children etc.), so no recursion required.

0

Oh, that's cool, I heard about it but didn't use it. I wasn't sure if it reach to the last child.

1 answer

I think it's not working because you are calling the recurse() function within the same function. This is how your recurse function should be:

local function recurse(model)
   local d = model:GetDescendants() 
   for i,v in next,d do
       if v:IsA("Script") and v.Name == "KillScript" then
           v.Disabled = true
      end
end

By the way: next,d is the same as pairs(d) its just an easier way

2 comments

0

I don't think there is a difference between using :GetChildren() as many times until there is no part and using one :GetDescendent()

This discussion is preserved as it was. New answers, comments, and votes are closed.