The result returns as nil for no reason

Task.Attack = function(Attacker: Player)
	local AttackerCharacter: Model = Attacker.Character
	local AttackerRightArm: BasePart =  AttackerCharacter:FindFirstChild("Right Arm")
	
	local Connection: RBXScriptConnection
	Connection = AttackerRightArm.Touched:Connect(function(TouchedPart: BasePart)
		local TouchedCharacter: Model = TouchedPart:FindFirstAncestorOfClass("Model")
		if TouchedCharacter then
			Connection:Disconnect()
			
			local TouchedPlayer: Player = Players:GetPlayerFromCharacter(TouchedCharacter)
			if TouchedPlayer then
				print(TouchedPlayer)
				
				return TouchedPlayer
			end
		end
	end)
	
	task.wait(0.25)
	Connection:Disconnect()
end

^^ this is a part of the module script

AttackEvent.OnServerEvent:Connect(function(FiredPlayer: Player)
	local VictimPlayer = Task.Attack(FiredPlayer)
	print(VictimPlayer)
	
	if VictimPlayer then
		Task.Execute(FiredPlayer, VictimPlayer)
		
	end
end)

server script

victim player returns as nil but the instance is there before it returned

1 Like

i think u forgot to return the Victim Player

2 Likes

sorry i didnt include that part of the code lemme reedit

I see, basically u are returning the TouchedPlayer inside the Touched event, which only returns to the connected function, not the actual Attack function. There are actually alot of options to solve it, an example would be adding a function to the second argument which will be execute if TouchedPlayer is found e.g

Task.Attack = function(Attacker: Player, func)
	local AttackerCharacter: Model = Attacker.Character
	local AttackerRightArm: BasePart =  AttackerCharacter:FindFirstChild("Right Arm")
	
	local Connection: RBXScriptConnection
	Connection = AttackerRightArm.Touched:Connect(function(TouchedPart: BasePart)
		local TouchedCharacter: Model = TouchedPart:FindFirstAncestorOfClass("Model")
		if TouchedCharacter then
			Connection:Disconnect()
			
			local TouchedPlayer: Player = Players:GetPlayerFromCharacter(TouchedCharacter)
			if TouchedPlayer then
				print(TouchedPlayer)
				
				func(TouchedPlayer)
			end
		end
	end)
	
	task.wait(0.25)
	Connection:Disconnect()
end
AttackEvent.OnServerEvent:Connect(function(FiredPlayer: Player)
	Task.Attack(FiredPlayer, function(TouchedPlayer) 
        
        Task.Execute(FiredPlayer, VictimPlayer)

    end
end)
1 Like

oh wow i never knew you could put functions in it

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