How can I make the Trail part stick to the ground

  1. What do you want to achieve? A sort of fire trail ability that’s controlled by the player’s mouse

  2. What is the issue? I was able to set up a range for it so it doesn’t go too far away but I can’t seem to make it stick to the ground, like it can go into the air and etc which is not what im looking for

  3. What solutions have you tried so far? I tried isolating the Y value and setting to 0 but that caused problems with uneven surfaces.

here’s the code that controls the trail part

-- script by mcximcff#2264
---
-- fire trail spell
---
-- 2/24/22

local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character

local fire = replicatedStorage.FireTrailSpell

local connection

--	if player:GetRankInGroup(11133567) == 255 or player:GetRankInGroup(11133567) == 205 then

userInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.X then

		if player:GetRankInGroup(11133567) == 205 or player:GetRankInGroup(11133567) == 255 then

			fire:FireServer("On")

			local BodyGyro

			fire.OnClientEvent:Connect(function()

				local mouse = player:GetMouse()

				BodyGyro = Instance.new("BodyGyro")
				BodyGyro.MaxTorque = Vector3.new(300, 300, 300)
				BodyGyro.P = 10^7
				BodyGyro.D = 100
				BodyGyro.Parent = character:FindFirstChild("HumanoidRootPart")


				connection = runService.RenderStepped:Connect(function()
					runService.RenderStepped:Wait()
					local Direction = Vector3.new(mouse.Hit.Position.X - character.HumanoidRootPart.Position.X, 0, mouse.Hit.Position.Z - character.HumanoidRootPart.Position.Z)
					BodyGyro.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + Direction)
					local Trail = workspace:FindFirstChild(player.Name.."'s Trail")
					mouse.TargetFilter = Trail
					if Trail then
						local range = math.min(30, (character.HumanoidRootPart.Position - mouse.Hit.p).Magnitude)
						local direction = (mouse.Hit.p - character.HumanoidRootPart.Position).Unit
						Trail.CFrame = CFrame.new(character.HumanoidRootPart.Position + direction * range)
					end
				end)
			end)

			wait(20)

			BodyGyro:Destroy()
			connection:Disconnect()
			fire:FireServer("Off")

		end
	end
end)	

all help is greatly appreciated

Does mouse.Hit.Position.Y not work?

no it works but when there’s uneven surfaces it wont go above 0 and it’ll just be stuck-

Ok, I’m working on a solution right now I’ll help you when I solve it

1 Like

Tell me if this works I don’t fully understand what you are trying to do so if it doesn’t work try playing around with some of the code or ask me :slight_smile:

local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

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

local fire = replicatedStorage.FireTrailSpell

local connection

local camera = workspace.CurrentCamera
local range = 30
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Whitelist
RayParams.FilterDescendantsInstances = {workspace.Baseplate,workspace.Ground} --insert the parts that you want the fire to appear on in here (these are just examples)

--	if player:GetRankInGroup(11133567) == 255 or player:GetRankInGroup(11133567) == 205 then

local function getWorldMousePos()
	local mousePos = userInputService:GetMouseLocation()
	local ray = camera:ViewportPointToRay(mousePos.X,mousePos.Y)
	local direction = ray.Direction * range
	local result = workspace:Raycast(ray.Origin, direction, RayParams)
	if result then
		return result.Position
	else
		return nil --return nothing because it isn't touching ground
	end
end

userInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if input.KeyCode == Enum.KeyCode.X then

		if player:GetRankInGroup(11133567) == 205 or player:GetRankInGroup(11133567) == 255 then

			fire:FireServer("On")

			local BodyGyro

			fire.OnClientEvent:Connect(function()
				BodyGyro = Instance.new("BodyGyro")
				BodyGyro.MaxTorque = Vector3.new(300, 300, 300)
				BodyGyro.P = 10^7
				BodyGyro.D = 100
				BodyGyro.Parent = character:FindFirstChild("HumanoidRootPart")

				connection = runService.RenderStepped:Connect(function()
					runService.RenderStepped:Wait()
					local WrldMousePos = getWorldMousePos()
					if WrldMousePos then
						local Direction = Vector3.new(WrldMousePos.X - character.HumanoidRootPart.Position.X, WrldMousePos.Y, WrldMousePos.Z - character.HumanoidRootPart.Position.Z)
						BodyGyro.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + Direction)
						local Trail = workspace:FindFirstChild(player.Name.."'s Trail")
						table.insert(RayParams.FilterDescendantsInstances,Trail)
						if Trail then
							local range = math.min(30, (character.HumanoidRootPart.Position - WrldMousePos).Magnitude)
							local direction = (WrldMousePos - character.HumanoidRootPart.Position).Unit
							Trail.CFrame = CFrame.new(character.HumanoidRootPart.Position + direction * range)
						end
					end
				end)
			end)

			wait(20)

			BodyGyro:Destroy()
			connection:Disconnect()
			fire:FireServer("Off")

		end
	end
end)	
1 Like

This worked perfectly! Thanks so much x

1 Like