How to return boolean value from module script to server script?

Hello! I was wondering if it’s possible to return a boolean value from a module script back to the server script it was called from to make something happen. (In my case, if the hit validation is true it fires the knockback module.) I haven’t played with module scripts much other than reusing blocks of code but I believe there’s even more you can do with them. In my script I’ve left comments on what I’m trying to achieve, thanks as always!

Server Script:

local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local abilityEvents = RS:WaitForChild("Ability")
local abilityRemote = abilityEvents:WaitForChild("AbilityRemote")
local abilityVFX = abilityEvents:WaitForChild("AbilityVFX")
local abilityFunction = abilityEvents:WaitForChild("AbilityFunction")

local validationModule = require(SS:WaitForChild("HitValidation"))

abilityRemote.OnServerEvent:Connect(function(player, vTable)
	
	validationModule.HitValidation(player, vTable, abilityVFX, 3, 6, 75) --Validation is sent to a module script 
	
	--[[if validHit == true then
		fire knockback module
	end]]
	
end)

Module Script:

local module = {}

local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local abilityEvents = RS:WaitForChild("Ability")
local abilityRemote = abilityEvents:WaitForChild("AbilityRemote")
local abilityFunction = abilityEvents:WaitForChild("AbilityFunction")

local debounce = {}

function module.HitValidation(player, vTable, abilityVFX, c, m, a)
	
	local validHit = false -- I want to return this value to the server script that called it
	
	local cooldown = c
	local hitChars = {}

	local pChar = player.Character
	local hrp = pChar:FindFirstChild("HumanoidRootPart")
	local pos = hrp.Position
	local pDirection = hrp.CFrame.LookVector

	if table.find(debounce, player.Name) ~= nil then --Debounce cooldown so exploiters cannot spam the server with requests
		print("Under debounce!")
	else
		print("Ability casted!")
		table.insert(debounce, player.Name)

		for _, vChar in pairs (vTable) do --Checks if hit was actually valid

			local vrp = vChar:FindFirstChild("HumanoidRootPart")
			local vPos = vrp.Position

			local magCheck = (vPos - pos)
			local distance = magCheck.Magnitude --Gets distance between the players
			local normalizedDist = magCheck.Unit --Normalizes that distance number

			local dot = pDirection:Dot(normalizedDist)  --Normalizes look vector
			local radAngle = math.acos(dot) --Finds the angle between the vrp and player look direction
			local degAngle = math.deg(radAngle) --Converts above into an angle

			if distance <= m and degAngle <= a and vChar and not table.find(hitChars, vChar) then --Validation check
				print("Hit")
				table.insert(hitChars, vChar) --Adds player to table for VFX purposes
				vChar.Humanoid:TakeDamage(10)
				
				validHit = true
			end	
		end

		abilityVFX:FireAllClients(pChar, hitChars) --VFX

		task.wait(cooldown)
		debounce[table.find(debounce, player.Name)] = nil

	end
	return validHit
end

return module
1 Like

This already works, you just need to do this:


local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local abilityEvents = RS:WaitForChild("Ability")
local abilityRemote = abilityEvents:WaitForChild("AbilityRemote")
local abilityVFX = abilityEvents:WaitForChild("AbilityVFX")
local abilityFunction = abilityEvents:WaitForChild("AbilityFunction")

local validationModule = require(SS:WaitForChild("HitValidation"))

abilityRemote.OnServerEvent:Connect(function(player, vTable)
	
	local validHit = validationModule.HitValidation(player, vTable, abilityVFX, 3, 6, 75) --Validation is sent to a module script 
	
	if validHit == true then
		


	end
	
end)

Or you can do this:


local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local abilityEvents = RS:WaitForChild("Ability")
local abilityRemote = abilityEvents:WaitForChild("AbilityRemote")
local abilityVFX = abilityEvents:WaitForChild("AbilityVFX")
local abilityFunction = abilityEvents:WaitForChild("AbilityFunction")

local validationModule = require(SS:WaitForChild("HitValidation"))

abilityRemote.OnServerEvent:Connect(function(player, vTable)
	
	if validationModule.HitValidation(player, vTable, abilityVFX, 3, 6, 75) then
		


	end
	
end)

Oh I had no idea you could do that! This definitely opens a ton of possibilities in my work.

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