How can i get a variable from a function?

i know there’s loads of topics on this but i just can’t seem to get it.

im trying to retrieve my otherplr variable from the top function but im not sure how to do it. i appreciate any help

pickupPlayerEvent.OnServerEvent:Connect(function(myPlr, myHRP:MeshPart)
	for _, otherPlr in ipairs(game.Players:GetPlayers()) do
		local otherCharacter = otherPlr.Character
		if myPlr == otherPlr then continue end
		if not otherCharacter then continue end
		local otherHRP:MeshPart = otherCharacter:WaitForChild("HumanoidRootPart")
		if not otherHRP then continue end
		
		local dist = (otherHRP.Position - myHRP.Position).Magnitude
		if dist < 10 then
			print(string.format("%s is close (%f studs away)", otherPlr.Name, dist))
			local module = require(script.pickupModule)
				module.stallPlayer(otherPlr)
				wait()
				module.createWeld(otherPlr, myPlr.Character)
		end
	end
end)

DropPlayerEvent.OnServerEvent:Connect(function(myPlr)
	local module = require(script.pickupModule)
	module.dropPlayer(otherplr, myPlr)
end)

does this work?

local currentPlr = nil

pickupPlayerEvent.OnServerEvent:Connect(function(myPlr, myHRP:MeshPart)
	for _, otherPlr in ipairs(game.Players:GetPlayers()) do
		local otherCharacter = otherPlr.Character
		if myPlr == otherPlr then continue end
		if not otherCharacter then continue end
		local otherHRP:MeshPart = otherCharacter:WaitForChild("HumanoidRootPart")
		if not otherHRP then continue end
		
		currentPlr = otherPlr
		
		local dist = (otherHRP.Position - myHRP.Position).Magnitude
		if dist < 10 then
			print(string.format("%s is close (%f studs away)", otherPlr.Name, dist))
			local module = require(script.pickupModule)
			module.stallPlayer(otherPlr)
			wait()
			module.createWeld(otherPlr, myPlr.Character)
		end
	end
end)

DropPlayerEvent.OnServerEvent:Connect(function(myPlr)
	if currentPlr then
		local module = require(script.pickupModule)
		module.dropPlayer(otherplr, myPlr)
	end
end)
1 Like

just say: otherplr = nil

and then later in the pickupplayerEvent set it to something existing

1 Like

hey man, thanks a million for your help. i ended up figuring it out after a while. my way is prolly longer than normal but oh well. ty man!

ty for ur help bro i got it. i tried that at the beginning but i have no idea why it didnt work. i stumbled my way along and got it to work tho so ty

yeah no problem! im just wondering if mine didnt work because it looks the same as my code with the same outcome or am i wrong?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.