WaitForChild() broken after player death

I made some scripts that let my player weld/un-weld parts to my face when Q is pressed, it works great but when I die or reset, the script can’t find my player’s parts.
I tried making a separate script that reloads the character after it dies, but it didn’t fix anything.
here is the beginning of my script:

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
wait(5)
local character = players.LocalPlayer.character
local root = character:FindFirstChild("HumanoidRootPart", true)
local jaw = character:FindFirstChild("Grip")

local isHolding = script.IsHolding.Value


userInputService.InputBegan:Connect(function(input, gameProcessedEvent) -- Q keypress, calls find or drop function		if input.UserInputType == Enum.UserInputType.Keyboard then 
	if input.KeyCode == Enum.KeyCode.Q then -- waits for player to press "Q"		
		--print (character, "Is Holding:", isHolding)					
		if isHolding == false then -- determines if key press will pick up new item or drop current item			
			findObj() -- call pick up function								
		else 								
			dropObj() -- call drop function							
		end					
	end		
end)	





function findObj() -- find obj to pick up
	local regionSearch = character:WaitForChild("RegionSearch")
	
	local function GetTouchingParts(regionSearch) -- make a table of objects inside of block
		local connection = regionSearch.Touched:Connect(function() end)
		local results = regionSearch:GetTouchingParts()
		connection:Disconnect()
		return results
	end
	local results = GetTouchingParts(regionSearch)
	--print("Parts found:", #results)
	
	for count = 1, 4 do -- remove unqualified parts from the table, needs to repeat because it only works half of the time.
		for i, v in pairs(results) do 
			if v:IsDescendantOf(character) == true then -- remove own body parts
				table.remove(results, i)	
				else if v == game.Workspace.Terrain then -- remove terrain
					table.remove(results, i)
				end
			end	
		end					
	end
	
	--print("FINALS:", #results) 
	for i, v in pairs(results) do -- the list of parts that can be grabbed
		--print(v)
	end
	
	while #results ~= 1 and #results ~= 0 do -- narrow the list down to one part
		for i, v in pairs(results) do -- loop this code until only one part remains
			if results[(i + 1)] ~= nil then -- see if there are two parts to compare
				local distance = (v.Position - jaw.Position).magnitude -- the distances from the item to the jaw		
				local distance2 = ((results[(i + 1)]).Position - jaw.Position).magnitude		
				--print("1:", distance)			
				--print("2:", distance2)			
				if (distance) > (distance2) then --delete the first item if it is farther away					
					--print("DELETE 1")					
					table.remove(results, i)					
				end			
				if (distance) < (distance2) then --delete the second item if it is farther away		
					--print("DELETE 2")
					table.remove(results, (i + 1))	
				end	
			end	
		end
	end
	
	--print("PARTS LEFT:")
	for i, v in pairs(results) do -- should give only one output
		--print(v)
	end
	if #results == 1 then
		local choice = results[1]
		local location = choice.Position
		grabObj(choice, location) -- call grab funcion
	end
end

function grabObj(object, location) -- find a spot to grab the object, then fire a server event
	local lastray = Ray.new(jaw.Position, (location - jaw.Position))
	
	local function getIntersection(object, lastray) -- use a ray to find a position on the surface of the object
		local whitelist = {object}
		local hit, position, normal = workspace:FindPartOnRayWithWhitelist(lastray, whitelist)
		return hit, position, normal
	end
	local hit, position, normal = getIntersection(object, lastray)		
	local change = (position - location) -- the ammount of space that the object needs to move to reach my face
	
	if hit ~= nil then -- 
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local GrabEvent = ReplicatedStorage:WaitForChild("grabObjEvent")
		
		GrabEvent:FireServer(hit, change, jaw)
		isHolding = true
	end
end


function dropObj() -- call the destroy function through an event
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local DropEvent = ReplicatedStorage:WaitForChild("dropObjEvent")
	

	DropEvent:FireServer(jaw)
	isHolding = false
end


this is the serverscript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GrabEvent = game.ReplicatedStorage:WaitForChild("grabObjEvent")
local DropEvent = game.ReplicatedStorage:WaitForChild("dropObjEvent")

local function onGrabEventFired(player, object, change, jaw)
	print(player.name, "grabbed", object)
	object.CFrame = CFrame.new(jaw.Position - change)
	
	local weld = Instance.new("WeldConstraint")
	weld.Parent = jaw
	weld.Part0 = jaw
	weld.Part1 = object
	
end

local function onDropEventFired(player, jaw)
	local weld = jaw.WeldConstraint
	local name = weld.Part1
	jaw.WeldConstraint:remove()
	print(player.name, "Dropped", name)
end

DropEvent.OnServerEvent:Connect(onDropEventFired)
GrabEvent.OnServerEvent:Connect(onGrabEventFired)

if looking at the game helps, you can play it here

edit: put in rest of localscript and the serverScript

Where is this stored in the game?

that script is stored in starterPlayerScripts, should i give the server script too?

Is it a server script or local script?

Player.CharacterAdded:Connect(function(Char)
    character = Char
    Head = Character:WaitForChild("Head")
end)

Something like this should work!

1 Like

Thanks!
I added

Player.CharacterAdded:Connect(function(Char)
    character = Char
	root = character:WaitForChild("HumanoidRootPart")
	jaw = character:WaitForChild("Grip")
	local regionSearch = character:WaitForChild("RegionSearch")
end)

and the problem is fixed as far as I know

1 Like