Raycasting script applies for players but not morphs

Hey! In the game I’m working on there’s a morphing system. And I made a server script so that when the raycasting detects something above the player it fires an event that disables the snow in the map on the client side. Now, as the title suggests it only works for the player and does not work for the morph.

-- Get the particle system from the workspace
local particle1 = game.Workspace.CameraPart.ParticleEmitter
local particle2 = game.Workspace.CameraPart.ParticleEmitter2
local particle3 = game.Workspace.CameraPart.Particles

local particle4 = game.Workspace.CameraPart2.ParticleEmitter
local particle5 = game.Workspace.CameraPart2.SnowFlake
local replicatedEvent = game.ReplicatedStorage:FindFirstChild("MyCustomEvent")

OldP1Value = particle1.Transparency
OldP2Value = particle2.Transparency
OldP3Value = particle3.Transparency
OldP4Value = particle4.Transparency
OldP5Value = particle5.Transparency

-- Define a function to check if a player is under any part using raycasting
local function checkPlayerUnderPart(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		local raycastResult = workspace:Raycast(humanoidRootPart.Position, Vector3.new(0,-1,0)*100)
		if raycastResult and raycastResult.Instance:IsA("BasePart") then
			-- Disable the particle system if the player is under a part
			replicatedEvent:FireClient(player, "disableParticle")
		else
			-- Enable the particle system if the player is not under any part
			replicatedEvent:FireClient(player, "enableParticle")
		end
	end
end

-- Connect the function to the player's character
game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	coroutine.wrap(function()
		while wait(0.1) do
			local raycastParams = RaycastParams.new();
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist;
			raycastParams.FilterDescendantsInstances = {player.Character};
			local ray = workspace:Raycast(character:WaitForChild("HumanoidRootPart").Position, Vector3.new(0, 15, 0) * 100, raycastParams);
			if ray and ray.Instance:IsA("BasePart") then
				-- If the player is under any part, call the function to disable the particle system
				checkPlayerUnderPart(player)
			else
				-- If the player is not under any part, enable the particle system
				replicatedEvent:FireClient(player, "enableParticle")
			end
		end
	end)();
end)

1 Like

Hey! Can I see the children under your character’s module after it morphs?
Also, I recommend printing.

Where do I find the character module? Do you mean model?

Yeah sorry, I meant model lol. (I’m half asleep)

Your problem should be this part of the code:

	local character = player.Character or player.CharacterAdded:Wait()

Why? Your resetting the character when it morphs, and not checking for a new character. Simple fix.

-- Get the particle system from the workspace
local particle1 = game.Workspace.CameraPart.ParticleEmitter
local particle2 = game.Workspace.CameraPart.ParticleEmitter2
local particle3 = game.Workspace.CameraPart.Particles

local particle4 = game.Workspace.CameraPart2.ParticleEmitter
local particle5 = game.Workspace.CameraPart2.SnowFlake
local replicatedEvent = game.ReplicatedStorage:FindFirstChild("MyCustomEvent")

OldP1Value = particle1.Transparency
OldP2Value = particle2.Transparency
OldP3Value = particle3.Transparency
OldP4Value = particle4.Transparency
OldP5Value = particle5.Transparency

-- Define a function to check if a player is under any part using raycasting
local function checkPlayerUnderPart(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		local raycastResult = workspace:Raycast(humanoidRootPart.Position, Vector3.new(0,-1,0)*100)
		if raycastResult and raycastResult.Instance:IsA("BasePart") then
			-- Disable the particle system if the player is under a part
			replicatedEvent:FireClient(player, "disableParticle")
		else
			-- Enable the particle system if the player is not under any part
			replicatedEvent:FireClient(player, "enableParticle")
		end
	end
end

-- Connect the function to the player's character
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		coroutine.wrap(function()
			while wait(0.1) do
				local raycastParams = RaycastParams.new();
				raycastParams.FilterType = Enum.RaycastFilterType.Blacklist;
				raycastParams.FilterDescendantsInstances = {player.Character};
				local ray = workspace:Raycast(character:WaitForChild("HumanoidRootPart").Position, Vector3.new(0, 15, 0) * 100, raycastParams);
				if ray and ray.Instance:IsA("BasePart") then
					-- If the player is under any part, call the function to disable the particle system
					checkPlayerUnderPart(player)
				else
					-- If the player is not under any part, enable the particle system
					replicatedEvent:FireClient(player, "enableParticle")
				end
			end
		end)();
	end)
end)

This should help, lmk if you need anything else.

I don’t know but it still doesn’t make the particles disappear when I’m morphed

Can I see the script where you morph?

The problem is not with the morphing script because a lot of other scripts work with the morph it’s just this one. And I’m scared of breaking the morph script

Here, try this:


-- Get the particle system from the workspace
local particle1 = game.Workspace.CameraPart.ParticleEmitter
local particle2 = game.Workspace.CameraPart.ParticleEmitter2
local particle3 = game.Workspace.CameraPart.Particles

local particle4 = game.Workspace.CameraPart2.ParticleEmitter
local particle5 = game.Workspace.CameraPart2.SnowFlake
local replicatedEvent = game.ReplicatedStorage:FindFirstChild("MyCustomEvent")

OldP1Value = particle1.Transparency
OldP2Value = particle2.Transparency
OldP3Value = particle3.Transparency
OldP4Value = particle4.Transparency
OldP5Value = particle5.Transparency

-- Define a function to check if a player is under any part using raycasting
local function checkPlayerUnderPart(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		local raycastResult = workspace:Raycast(humanoidRootPart.Position, Vector3.new(0,-1,0)*100)
		if raycastResult and raycastResult.Instance:IsA("BasePart") then
			-- Disable the particle system if the player is under a part
			replicatedEvent:FireClient(player, "disableParticle")
		else
			-- Enable the particle system if the player is not under any part
			replicatedEvent:FireClient(player, "enableParticle")
		end
	end
end

-- Connect the function to the player's character
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(char)
		coroutine.wrap(function()
			while wait(0.1) do
				if player.Character ~= nil then
					local character = player.Character
					local raycastParams = RaycastParams.new();
					raycastParams.FilterType = Enum.RaycastFilterType.Blacklist;
					raycastParams.FilterDescendantsInstances = {player.Character};
					local ray = workspace:Raycast(character:WaitForChild("HumanoidRootPart").Position, Vector3.new(0, 15, 0) * 100, raycastParams);
					if ray and ray.Instance:IsA("BasePart") then
						-- If the player is under any part, call the function to disable the particle system
						checkPlayerUnderPart(player)
					else
						-- If the player is not under any part, enable the particle system
						replicatedEvent:FireClient(player, "enableParticle")
					end
				end
			end
		end)();
	end)
end)

Thank you so muchhh!! It worked!

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