RemoteFunction not returning a value

The code below is suppose to return a value to the client via a remote function as the title says,

remotes.hit.OnServerInvoke = function(player,debounce,repetition)
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if hum.Health>0 and debounce.Value == false then
	debounce.Value = true
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://"..repetition.animationId
	local track = hum:LoadAnimation(anim);track:Play()
	local hitBox,touchedEvent
	track.KeyframeReached:Connect(function(key)
		if key == "Stop0" then
			debounce.Value = false
		end
	end)
	track.Stopped:Connect(function()
		hitBox=hitBox and hitBox:Destroy()
		touchedEvent=touchedEvent and touchedEvent:Disconnect()
	end)
	if not(repetition.hitBox==nil) then
		hitBox=Instance.new("Part");hitBox.Name="hitBox";hitBox.Size=Vector3.new(2,2,2);hitBox.Transparency=1;hitBox.CanCollide=false
		local weld = Instance.new("Weld");weld.Part0=char[repetition.hitBox];weld.Part1=hitBox;weld.Parent=hitBox
		weld.Parent=hitBox;hitBox.Parent=char;
	end
	touchedEvent = hitBox.Touched:Connect(function(hit)
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health>0 and hit.Parent~=char then
			touchedEvent:Disconnect()
			if repetition.hitId then
				local react = Instance.new("Animation")
				react.AnimationId = "rbxassetid://"..repetition.hitId
				local reaction = hit.Parent.Humanoid:LoadAnimation(react)
				reaction:Play(nil,nil,2)
			end
			print("nani")
			return true
		end
	end)
end
end

It, somehow, prints nani but when I try to print the returned value on the client it simply prints nil :thinking:

local debounce = script.Parent:WaitForChild("States"):WaitForChild("Attack")
function process(input,gameProcessedEvent)
	timeComplexity.bindable:Fire()
	playSlashSound()
	local hit = remotes.hit:InvokeServer(debounce,combos_chain[indexes.bigIndex][indexes.subIndex])
	print(hit) --says nil...
	counterManager(combos_chain[indexes.bigIndex][indexes.subIndex],hit)
end

uis.InputBegan:Connect(function(input,gameProcessedEvent)
	if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
		local disabledByTag = simplicity.isTagged(char,{"ice","poison","lightning"})
		local bounce = remotes.request:InvokeServer(debounce)
		if disabledByTag == false and bounce == true then debounce.Value = true
			process(input,gameProcessedEvent)
		end
	end
end)
1 Like

I think I see the problem here.

The return statement after your “nani” print is returning out of the Touched event handler, not the invocation handler. You can fix this by creating a BindableEvent outside of your Touched event handler and firing it instead of returning true inside the event handler. After your touch event handler, just :Wait() for your BindableEvent’s event to fire and then return true.

...OnServerInvoke = function(player)
...
local be = Instance.new("BindableEvent")
touchedEvent = hitBox.Touched:Connect(function(hit)
    ...
    print("nani")
    be:Fire()
end)

be:Wait()
return true
end

Something like this should fix your problem.

1 Like

Tried to but it doesn’t seem to work or I did something wrong?

remotes.hit.OnServerInvoke = function(player,debounce,repetition)
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	if hum.Health>0 and debounce.Value == false then
		debounce.Value = true
		local anim = Instance.new("Animation")
		anim.AnimationId = "rbxassetid://"..repetition.animationId
		local track = hum:LoadAnimation(anim);track:Play()
		local hitBox,touchedEvent
		track.KeyframeReached:Connect(function(key)
			if key == "Stop0" then
				debounce.Value = false
			end
		end)
		track.Stopped:Connect(function()
			hitBox=hitBox and hitBox:Destroy()
			touchedEvent=touchedEvent and touchedEvent:Disconnect()
		end)
		if not(repetition.hitBox==nil) then
			hitBox=Instance.new("Part");hitBox.Name="hitBox";hitBox.Size=Vector3.new(2,2,2);hitBox.Transparency=1;hitBox.CanCollide=false
			local weld = Instance.new("Weld");weld.Part0=char[repetition.hitBox];weld.Part1=hitBox;weld.Parent=hitBox
			weld.Parent=hitBox;hitBox.Parent=char;
		end
		local bindableReturn = Instance.new("BindableEvent")
		touchedEvent = hitBox.Touched:Connect(function(hit)
			if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health>0 and hit.Parent~=char then
				touchedEvent:Disconnect()
				if repetition.hitId then
					local react = Instance.new("Animation")
					react.AnimationId = "rbxassetid://"..repetition.hitId
					local reaction = hit.Parent.Humanoid:LoadAnimation(react)
					reaction:Play(nil,nil,2)
				end
				print("bruh")
				bindableReturn:Fire()
			end
		end)
		local result = bindableReturn.Event:Wait()
		bindableReturn:Destroy()
		print("sighs")
		return result
	end
end

I believe you need to put true inside of the :Fire() call under “bruh”. Right now it looks like it’s just always returning nil. Also, is it printing “sighs” at all?

2 Likes

Thank you so much lol I forgot about it.

2 Likes