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
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)