Switching between piggy and players during round

Hi, I’m trying to make it so that when the player touches the door, they will switch from being a contestant to being a piggy and the current piggy will turn into a contestant (a normal player). The problem is when I attempt to do that, it ends the whole game as the game thinks the piggy left. Here is my code:

Door Code

function module.ActivateDoors(doorFolder)
	for _, door in pairs(doorFolder:GetChildren()) do
		if door:FindFirstChild("Key") then
			
			door.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Key") then
					if door.Key.Value == hit.Parent.Name then
						if door.Name == "ExitDoor" then
							if door.Generator.On.Value == true and not door:FindFirstChild("WoodBlock") then
								door.Padlock.Anchored = false
								wait(1)
								door.Transparency = 1
								door.CanCollide = false
								
								door.Touched:Connect(function(hit)
									
									local player = game.Players:GetPlayerFromCharacter(hit.Parent)
									
									if player then
										if player:FindFirstChild("Contestant") and not player:FindFirstChild("Escaped") then
											
											local Escaped = Instance.new("BoolValue")
											Escaped.Name = "Escaped"
											Escaped.Parent = player
											
											game.ReplicatedStorage.Announcement:FireClient(player,"You Escaped")
											
										end
									end
									
										
								end)
								
							end
						else
							door.Padlock.Anchored = false
							wait(0.5)
							door:Destroy()
							
							for i, v in pairs(game.Players:GetPlayers()) do
								if v:FindFirstChild("Piggy") then
									local map = game.Workspace:FindFirstChild("Map")
									local mapSpawns = map.PlayerSpawns:GetChildren()
									v.Piggy:Destroy()
									v:LoadCharacter()
									wait(1)
									RoundModule.InsertNewTag(v, "Contestant")
									wait(.1)
									RoundModule.TeleportPlayers(v, mapSpawns)
									print("Piggy: ".. v.Name.. "is now a contestant")
								end
								
							end

Teleport Players

function module.TeleportPlayers(players, mapSpawns)
	for i, player in pairs(players) do
		if player.Character then
			local character = player.Character
			
			if character:FindFirstChild("HumanoidRootPart") then
				player.Character.Humanoid.WalkSpeed = 16
				
				local rand = Random.new()
				player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1, #mapSpawns)].CFrame + Vector3.new(0,10,0)
				
				local hitBoxClone = game.ServerStorage.Hitbox:Clone()
				hitBoxClone.CFrame = character.HumanoidRootPart.CFrame
				
				local weld = Instance.new("Weld")
				weld.Part1 = character.HumanoidRootPart
				weld.Part0 = hitBoxClone
				weld.Parent = character
				
				hitBoxClone.Parent = player.Character
			
			end
		end
	end
end

Insert Tag

function module.InsertNewTag(contestants, tagName)
		local Tag = Instance.new("StringValue")
		Tag.Name = tagName
		Tag.Parent = contestants	
end

Dress Piggy

function module.DressPiggy(piggy)
	local character = game.ServerStorage.Piggy:Clone()
	character.Name = piggy.Name
	piggy.Character = character
	character.Parent = workspace
end

Teleport Piggy

function module.TeleportPiggy(player)
	if player.Character then
		player.Character.Humanoid.WalkSpeed = 14
		local bat = game.ServerStorage.Tools.PiggyBat:Clone()
		bat.Parent = player.Character
		
		if player.Character:FindFirstChild("HumanoidRootPart") then
			player.Character.HumanoidRootPart.CFrame = game.Workspace.WaitingRoom.PiggyWaitingSpawn.CFrame + Vector3.new(0,5,0)
		end
		
		local TrapCount = Instance.new("IntValue")
		TrapCount.Name = "TrapCount"
		TrapCount.Value = 5
		TrapCount.Parent = player
		
		game.ReplicatedStorage.ToggleTrap:FireClient(player, true)
		
	end
end

Also this is adding onto the tutorial, the code seen below was created by me.

That’s what I need help with.

Would anyone know how to fix this?

Can you post the errors here? We can’t help if there are no errors

Don’t use tutorial code.
Don’t make Piggy games.

There’s your solution.

1 Like

lol it won’t be a piggy game. the tutorial was created by Alvin Blox

I think the problem is somewhere is game code as you are removing “Piggy” and replacing it with another one and when “Piggy” is removed or changed I think it triggers an event and then the script that handles it thinks the player left since when it ran “Piggy” does not exist.

That would be the problem, however, I don’t know how i’d fix it.

Here’s the error I’m receiving