Kill Effect only works when not using a ModuleScript

hello, ive been trying to use RayCastParams with a sword to affect a player. It works great. When i try to use it to do a kill effect it works. But when I use the kill effect in a module script, it always returns “RayCast not Found” when coming in contact with a player. It should return true because it does when im not using the ModuleScript.

The Sword Script (Without using the module)

local cooldowns = {}
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local goldParticle = ServerStorage:FindFirstChild("GoldParticle")
local goldSparkles = ServerStorage:FindFirstChild("GoldSparkles")
local sfx = ServerStorage:FindFirstChild("Ice Spawn SFX")



game.Players.PlayerAdded:Connect(function(player)

player.CharacterAdded:Connect(function(character)

		local m6d = Instance.new("Motor6D", character.RightHand)
		m6d.Part0 = character.RightHand
		m6d.Name = "ToolM6D"
	end)
end)

game.ReplicatedStorage.SwordRE.OnServerEvent:Connect(function(player, instruction, bodyAttach)

	if instruction == "connectm6d" then

		player.Character.RightHand.ToolM6D.Part1 = bodyAttach

	elseif instruction == "disconnectm6d" then

		player.Character.RightHand.ToolM6D.Part1 = nil

	elseif instruction == "attack" then

		if cooldowns[player] then return end

		cooldowns[player] = true

		game.ReplicatedStorage.SwordRE:FireAllClients(bodyAttach)

		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {player.Character}

		local raycastResults = workspace:Raycast(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.CFrame.LookVector * 3, raycastParams)

		if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then

			raycastResults.Instance.Parent.Humanoid:TakeDamage(30)
		end
		
		cooldowns[player] = false
		wait(0.5)
		local effects = {"Fire", "Smoke", "Gold"}	
		local equippedKill = Instance.new("StringValue")
		equippedKill.Name = "EquippedKillEffect"
		equippedKill.Parent = player
		equippedKill.Value = "Fire"
		local potentialWearables = {"Accessory", "Shirt", "Pants"}
		if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then
			warn("It should work ")
			raycastResults.Instance.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
				warn(":(")
				if raycastResults.Instance.Parent.Humanoid.Health <= 0 then
					print("Pog")
					for body, v in pairs(raycastResults.Instance.Parent:GetDescendants()) do
						if v:IsA("BasePart") or v:IsA("MeshPart") then
							v.BrickColor = BrickColor.new("Dark stone grey")
							v.Material = "Asphalt"
						end
						if v:IsA("Decal") or v:IsA("Accessory") or v:IsA("Shirt") or v:IsA("Pants") then 
							v:Destroy()
						end
					end
					local fire = Instance.new("Fire")
					fire.Parent = raycastResults.Instance.Parent.HumanoidRootPart
					fire.Color = Color3.new(0.286275, 0.00392157, 1)
				end
			end)
		else 
			error("RayCast was not Found!")
		end	
		end	
		
end)

Heres the script when I use the ModuleScript for the kill effect:

local cooldowns = {}
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local goldParticle = ServerStorage:FindFirstChild("GoldParticle")
local goldSparkles = ServerStorage:FindFirstChild("GoldSparkles")
local sfx = ServerStorage:FindFirstChild("Ice Spawn SFX")



game.Players.PlayerAdded:Connect(function(player)

player.CharacterAdded:Connect(function(character)

		local m6d = Instance.new("Motor6D", character.RightHand)
		m6d.Part0 = character.RightHand
		m6d.Name = "ToolM6D"
	end)
end)

game.ReplicatedStorage.SwordRE.OnServerEvent:Connect(function(player, instruction, bodyAttach)

	if instruction == "connectm6d" then

		player.Character.RightHand.ToolM6D.Part1 = bodyAttach

	elseif instruction == "disconnectm6d" then

		player.Character.RightHand.ToolM6D.Part1 = nil

	elseif instruction == "attack" then

		if cooldowns[player] then return end

		cooldowns[player] = true

		game.ReplicatedStorage.SwordRE:FireAllClients(bodyAttach)

		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {player.Character}

		local raycastResults = workspace:Raycast(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.CFrame.LookVector * 3, raycastParams)

		if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then

			raycastResults.Instance.Parent.Humanoid:TakeDamage(30)
		end
		
		cooldowns[player] = false
		wait(0.5)
		local effects = {"Fire", "Smoke", "Gold"}	
		local equippedKill = Instance.new("StringValue")
		equippedKill.Name = "EquippedKillEffect"
		equippedKill.Parent = player
		equippedKill.Value = "Fire"
		local potentialWearables = {"Accessory", "Shirt", "Pants"}
		local Fire = require(ServerScriptService:WaitForChild("ModuleScript"))
		if equippedKill then
			if Fire then
				if equippedKill.Value == (effects[1]) then
					local init = Fire.FireKillEffect()
				elseif equippedKill.Value == effects[2] then 
					require(ServerScriptService:WaitForChild(effects[2]))
					print("Smoke Has Been Loaded")
				elseif equippedKill.Value == effects[3] then 
					require(ServerScriptService:WaitForChild(effects[3]))
					print("Gold Has Been Loaded")
					end
				end
			end
		end	
		
end)

Heres the ModuleScript:

local  KillEffects = {}  

function KillEffects.FireKillEffect()
	if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then
		warn("It should work ")
		raycastResults.Instance.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			warn(":(")
			if raycastResults.Instance.Parent.Humanoid.Health <= 0 then
			print("Pog")
			for body, v in pairs(raycastResults.Instance.Parent:GetDescendants()) do
				if v:IsA("BasePart") or v:IsA("MeshPart") then
					v.BrickColor = BrickColor.new("Dark stone grey")
					v.Material = "Asphalt"
				end
				if v:IsA("Decal") or v:IsA("Accessory") or v:IsA("Shirt") or v:IsA("Pants") then 
					v:Destroy()
				end
			end
			local fire = Instance.new("Fire")
			fire.Parent = raycastResults.Instance.Parent.HumanoidRootPart
				fire.Color = Color3.new(0.286275, 0.00392157, 1)
			end
		end)
	else 
		error("RayCast was not Found!")
	end	
		return KillEffects.FireKillEffect()
end
function KillEffects.SmokeKillEffect()
	if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then
	raycastResults.Instance.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		if raycastResults.Instance.Parent.Humanoid.Health <= 0 then 
			for body, v in pairs(raycastResults.Instance.Parent:GetDescendants()) do
				if v:IsA("BasePart") or v:IsA("MeshPart") then
					v.BrickColor = BrickColor.new("Dark stone grey")
					v.Material = "Asphalt"
				end
				if v:IsA("Decal") or v:IsA("Accessory") or v:IsA("Shirt") or v:IsA("Pants") then 
					v:Destroy()
				end
			end
			local smoke = 	Instance.new("Smoke")
			smoke.Parent = raycastResults.Instance.Parent.HumanoidRootPart
			smoke.Color = Color3.new(0.752941, 1, 0.486275)
			smoke.TimeScale = 1
			smoke.RiseVelocity = 1
		end

		end)
	else 
		error("RayCast was not Found!")
	end
	return KillEffects.SmokeKillEffect()
end
function KillEffects.GoldKillEffect()
	if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then
raycastResults.Instance.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if raycastResults.Instance.Parent.Humanoid.Health <= 0 then 
		for body, v in pairs(raycastResults.Instance.Parent:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") then
				local sfxClone = sfx:Clone()
				sfxClone.Parent = raycastResults.Instance.Parent.HumanoidRootPart
				sfxClone:Remove()
				v.BrickColor = BrickColor.new("Gold")
				v.Material = "Foil"
			end
			if v:IsA("Accessory") or v:IsA("Shirt") or v:IsA("Pants") then 
				v:Destroy()
			end
		end
		local GPClone = goldParticle:Clone()
		GPClone.Parent = raycastResults.Instance.Parent.HumanoidRootPart
		local GSClone = goldSparkles:Clone()
		GSClone.Parent = raycastResults.Instance.Parent.HumanoidRootPart

		end
	end)
	else
		error("RayCast not Found!")
	end
		return KillEffects.GoldKillEffect()
end
return KillEffects

1 Like

You need to input your arguments into the function. Your module is trying to access variables that are nothing such as raycastResults. Just send it over as a param.
Fire.FireKillEffect(raycastResults)

4 Likes

The kill effect does work now but it still gives an error even after coming in contact with a player. Should i be returning anything in these brackets:
return KillEffects.FireKillEffect()

1 Like

I think you can omit the return statements after each function, keep the last one at the end though.

1 Like

Thank you so much! This worked great!

1 Like

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