Code not reaching certain lines

Hi, I’ve been trying to debug this code for the past 5-6 hours and still can’t figure out what needs to be fixed. What I am trying to do is make it so that if the player opens the door using a key, that player will become the new piggy and the current piggy will become a contestant. What the issue is is that the code seen below isn’t reached whatsoever. What ami I doing wrong? The full code is below the code.


							for i, player in pairs(game.Players:GetPlayers()) do
								local Player = game.Players:GetPlayerFromCharacter(hit.Parent:FindFirstChild("Key"))
								if Player then
									local NTBP = Player:FindFirstChild("NeedToBePiggy")
									if NTBP then
										local map = game.Workspace:FindFirstChild("Map")
										local mapSpawns = map.PlayerSpawns:GetChildren()
										Player.NeedToBePiggy:Destroy()
										print("NeedToBePiggyTag Destroyed")
										RoundModule.InsertNewTag(Player, "Piggy")
										print(" has new Piggy Tag")
										wait(.1)
										RoundModule.DressNewPiggy(Player)
										print("Player is now dressed as piggy")
										wait(.1)
										RoundModule.TeleportPiggy(Player, mapSpawns)
										print("Player is teleported to map")
			
									end		
								end
							end

Door Module Script

local module = {}

local tweenService = game:GetService("TweenService")
local RoundModule = require(script.Parent.RoundModule)

-- swing the door by `angle` degrees
function module.swingDoor(angle,door)
    -- locate the hinge on the door
    local hingeBefore = door.CFrame * CFrame.new(-2.5, 0, 0)

    -- relative to the hinge, where is the door?
    local relative = hingeBefore:toObjectSpace(door.CFrame)

    -- twist the hinge
    -- CONVERT angle *from* degrees *to* radians here
    local hingeAfter = hingeBefore * CFrame.Angles(0, math.rad(angle), 0)

    -- re-attach the door to the new "hinge"

	local tweenProperties = TweenInfo.new(0.2) -- Time
	local result = {CFrame = hingeAfter:toWorldSpace(relative)}
	
	door.CanCollide = false
	
	local tween = tweenService:Create(door,tweenProperties,result)
	tween:Play()
	
	wait(0.2)
	
	door.CanCollide = true
end

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()
							
								if hit.Parent:FindFirstChild("Key") then
					       	       local char = hit.Parent:FindFirstChild("Key")
					               local playerWhoTouched = game:GetService("Players"):GetPlayerFromCharacter(char)
					               print("Your name is : ")
									RoundModule.InsertNewTag(playerWhoTouched, "NeedToBePiggy")
									print("New tag added")
	           					end

							for i, player in pairs(game.Players:GetPlayers()) do
								local Player = game.Players:GetPlayerFromCharacter(hit.Parent:FindFirstChild("Key"))
								if Player then
									local NTBP = Player:FindFirstChild("NeedToBePiggy")
									if NTBP then
										local map = game.Workspace:FindFirstChild("Map")
										local mapSpawns = map.PlayerSpawns:GetChildren()
										Player.NeedToBePiggy:Destroy()
										print("NeedToBePiggyTag Destroyed")
										RoundModule.InsertNewTag(Player, "Piggy")
										print(" has new Piggy Tag")
										wait(.1)
										RoundModule.DressNewPiggy(Player)
										print("Player is now dressed as piggy")
										wait(.1)
										RoundModule.TeleportPiggy(Player, mapSpawns)
										print("Player is teleported to map")
			
									end		
								end
							end

							
							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()
									wait(2)
									v.Piggy:Destroy()
									v:LoadCharacter()
									wait(1)
									RoundModule.InsertNewTag(v, "Contestant")
									wait(.1)
									RoundModule.TeleportNewPlayer(v, mapSpawns)
									print("Piggy: ".. v.Name.. "is now a contestant")
								end
								
							end
							

							
							
						end
					end
				end
			end)
		
		else
			if not door:FindFirstChild("ClickDetector") then local cd = Instance.new("ClickDetector") cd.Parent = door end
			
			local debounce = false
			
			door:FindFirstChild("ClickDetector").MouseClick:Connect(function(player)
				
				if door.Name == "Door" then
					if not debounce then
						debounce = true
						
						local angle
						
						if door:FindFirstChild("Open") then
							angle = -90
							door.Open:Destroy()		
						else
							angle = 90	
							local openVal = Instance.new("StringValue")
							openVal.Name = "Open"
							openVal.Parent = door
						end
						
					
						module.swingDoor(angle,door)
						wait(.001)
						
						
						print("Door swung!")
						wait(1)
						debounce = false
					end
				end
			
			end)
		end
	end
end

return module

NOTE: This will not be a piggy game, the script that I have provided is an added on version, meaning I created parts from it that weren’t part of the tutorial. I really need help on this as it’s a main feature coming to the game. Any help will be appreciated!

One more thing to add: No errors occur, yeah, kinda made it a bajillion times harder to solve.

Do you need any additional scripts?

GetPlayerFromCharacter, as it’s named, searches for a player from a Character instance. hit.Parent can return a Character in a .Touched event, but searching for “Key” inside of a potential character won’t return a character, so GetPlayerFromCharacter will always be returning nil. Therefore the if Player then will never run

What should I do to fix that? Remove the line?

Change

local Player = game.Players:GetPlayerFromCharacter(hit.Parent:FindFirstChild("Key"))

to

local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
1 Like

Well what confuses me is why you’re looping through all of the players, and then trying to find a player instance with GetPlayerFromCharacter. What exactly are you trying to do here? There seems to be no use of the player variable from the in pairs loops.

I don’t really know what you’re trying to do here, but as @Quackers337 said, :GetPlayerFromCharacter(hit.Parent) can return a player instance.

Have you ever tryed to debug by printing, so you check where the code dont reach? and then remove that part to see if it works, maybe a loop or a additional wait you use.

Do you not see the prints? That’s what i’ve been doing.

Might be an issue with the control flow of your program. It looks like the code not being executed is in an else statement which only runs if the door’s name is not ‘ExitDoor’. Is this the desired behavior?

Well when you make the prints what is the last print you get so far in console?

Yes, I want it to be all the doors with locks except the exit

between this code
if hit.Parent:FindFirstChild(“Key”) then
local char = hit.Parent:FindFirstChild(“Key”)
local playerWhoTouched = game:GetService(“Players”):GetPlayerFromCharacter(char)
print("Your name is : ")
RoundModule.InsertNewTag(playerWhoTouched, “NeedToBePiggy”)
print(“New tag added”)
end

and your for loop, you tried to print something and still does not work or is when you pass your else you cant execute nothing? sorry but english is not my native language

Alright, well like Arroz suggested it might be helpful to use print statements for debugging. The only thing is they’re not useful if nothing gets printed, so place some in between the initial if statements of your function to make sure that the control flow gets to what you want to execute. And if you come across an error with the console thats also helpful to mention.

But change
local Player = game.Players:GetPlayerFromCharacter(hit.Parent:FindFirstChild(“Key”))
Key is not a character instance, should be the character. So if its a tool it should look like
local Player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)

  • useful to use print(Player) to check that your player is getting set right here

Then, you’ll want to make sure the player has the NeedToBePiggy element as a child.

Also I noticed you had nested touched events in the exitdoor statements, which is pretty not great. When a player touches the exit door, its going to fire 2+i events for every time the door is touched, where i is the previous number of times it has been touched.