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

How would I stop scripts from breaking when a player leaves at the exact time?

Recently I sponsored my game for 10k to find as many bugs as possible, and I found a bug that's way worse than I wanted to find. When a script is doing something to a player and they leave in that millisecond while that is happening, it breaks the script, therefore breaking the whole game. I added things like "if v" (v being the player) before every line that interacts with the player (v), but it still breaks sometimes. How would I stop this from happening?

A little bit of the code:

function TeleportZombies()
	local ZombiesLeft = 0
	for i,v in pairs(players:GetPlayers()) do
		if v then
			if v:FindFirstChild("Human") and v:FindFirstChild("IsPlaying") and v:FindFirstChild("TypeOfZombie") and v then
				if v.IsPlaying.Value == true and v then
					if v.Human.Value == false and v then
						ZombiesLeft = ZombiesLeft + 1
						rs.RoundInformation.ZombiesLeft.Value = ZombiesLeft
						if v then v.Team = teams.Zombies else return end
						local TeleportModel = workspace.CurrentMap.ZombieSpawns
						local num = 0
						for i,v in pairs(TeleportModel:GetChildren()) do
							num = num+1
						end
						local teleportpart = TeleportModel[math.random(1,num)].Position
						if v and v.Character then v.Character:MoveTo(teleportpart) end
						for i,a in pairs(ZombieWeapons:GetChildren()) do
							if a.Name == v.TypeOfZombie.Value then
								if v then 
									local bwep = a:Clone()
									local swep = a:Clone()
									bwep.Parent = v.Backpack
									swep.Parent = v.StarterGear
								end
							end
						end
						rs.Events3.PickZombieVisible:FireClient(v)
					else
						--[[
						for i,item in pairs(v.ItemsForNextRound:GetChildren()) do	
							if v then 
								item.Parent = v.Backpack
							end
						end
						--]]
					end
				else
				end
			end
		end
	end
end

1 comment

0

Care to show an example script?

1 answer

✓ Accepted answer

I see that you've mentioned trying to see if the player exists by doing

if plr then
	print('exists')
end

But this if statement will still error if he doesn't exists. The reason it still errors is because even before it checks if plr is not nil, plr is nil. It can't check if its true because the thing your giving the if statement is nil to begin with.

How do I check then? Well we need a method that even if the plr doesn't exist, will return false. We can use FindFirstChild() to do this

Below we try to find game.Players[playername] and if it exists return true else false. Unlike the nil we were giving it before.

if game.Players:FindFirstChild(playername)~=nil then
	print('exists')
end

How can I use this if I can't get the player's name anymore? You'll have to save their name before you begin doing things. So for example

--begin by saving their name
local plrn=plr.Name

--do your stuff
--suddenly they leave

--next task
if game.Players:FindFirstChild(playername)~=nil then
	print('do stuff')
end

You don't have to specifically save their name to a variable too, maybe you have an instance somewhere with their name. You just need to know their name to check using FindFirstChild.

1 comment

0

Thanks! I'll do this in my next game.

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