Problem with ModuleScript and RayCastParams.Instance

Hey im having trouble trying to make a kill effect by a sword activate when the afflicted player dies. It works when I don’t use it in a module script. It still gives the same error either way:

Without using a module script:


With using one:

Heres the Sword script(without using the module script for the 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"}
		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 fire = Instance.new("Fire")
			fire.Parent = raycastResults.Instance.Parent.HumanoidRootPart
			fire.Color = Color3.new(0.286275, 0.00392157, 1)
		end
	end)
	end	
end)

Heres the script when it uses 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"}
 local Fire = require(ServerScriptService:WaitForChild("ModuleScript"))
	if equippedKill then
		if Fire then
			if equippedKill.Value == (effects[1]) then
				local init = Fire.FireKillEffect()
				print("Fire Has Been Loaded")
			elseif equippedKill.Value == effects[2] then 
				print("1")
				require(ServerScriptService:WaitForChild(effects[2]))
				print("Smoke Has Been Loaded")
			elseif equippedKill.Value == effects[3] then 
				print("2")
				require(ServerScriptService:WaitForChild(effects[3]))
				print("Gold Has Been Loaded")
				end
			end
		end
	end	
	
end)

The Module Script:

local  KillEffects = {}

function KillEffects.FireKillEffect()
	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 fire = Instance.new("Fire")
			fire.Parent = raycastResults.Instance.Parent.HumanoidRootPart
			fire.Color = Color3.new(0.286275, 0.00392157, 1)
		end
	end)
	return KillEffects.FireKillEffect()
end

return KillEffects

You have to do an if/else statement to check if the raycast hit something, and if the instance exists.

1 Like

That suggestion did help the error without using a module script.
But when I use the if/else it always returns the else? Any ideas?