What am I doing wrong with RunService?

  1. What do you want to achieve?
    I’m using this code to detect when stones fired by a player’s skill are close enough to the ground, but often the stones simply cross the ground, or simply teleport to the final objective when they are far away.
								Event = RSE.Stepped:Connect(function(ElapsedTime, DeltaTime)

									if (Projectile.Position - RootPart.Position).Magnitude > SkillTable.Distance then

										if BV then 
											BV:Destroy()
											Direction = Vector3.new(0, -1, 0)
										end

									end
									
									local Cast : RaycastResult = workspace:Raycast(OldPos, -(OldPos - Projectile.Position).Unit * 999, Params)
									
									if Cast and Cast.Distance < 5 then
										
										print(Cast.Distance)
										
										if Event then Event:Disconnect() end
										if BV then BV:Destroy() end

										Projectile.Anchored = true
										Projectile.Position = Cast.Position

										local Detection = HitModule:HitBox(Character, {

											["Type"] = "Magnitude",
											["Range"] = SkillTable.Range,
											["CFrame"] = CFrame.new(Cast.Position),

										})
										
										local ShakeDetection = HitModule:HitBox(nil, {

											["Type"] = "Magnitude",
											["Range"] = 50,
											["CFrame"] = CFrame.new(Cast.Position),

										})

										if ShakeDetection then

											for TCharacter, Distance in pairs(ShakeDetection) do

												local TPlayer = PYS:GetPlayerFromCharacter(TCharacter)
												if TPlayer == nil then continue end

												Remotes.Effects:FireClient(TPlayer, "ShakeOnce", {

													["Duration"] = 5,
													["Preset"] = "Explosion"

												})

											end

										end

										Remotes.Effects:FireAllClients("GroundSmash", {

											["Position"] = Cast.Position,
											["Distance"] = 10,
											["MaxRocks"] = 15,
											["Size"] = Vector3.new(2, 2, 2),
											["Cracked"] = true,
											["Duration"] = 15

										})

										if Detection then

											local Hitted = HitModule:HitApply(Character, Detection, SkillTable.HitTable)

											if Hitted then

												for TCharacter, Table in pairs(Hitted) do

													local TRootPart = TCharacter:FindFirstChild("HumanoidRootPart")

													if Table["Fling"] and TRootPart then

														Table["Fling"].Part = TRootPart
														Table["Fling"].Direction = {Vector3.new(math.random(0, 25) / 100, 1, math.random(0, 25) / 100)}

														MainModule:Velocity(Table["Fling"])

													end

												end

											end

										end

										task.delay(15, function()

											local Tween = TSE:Create(Projectile, TweenInfo.new(1, Enum.EasingStyle.Linear), {["Size"] = Vector3.new(0.001, 0.001, 0.001)})
											Tween:Play()
											Tween.Completed:Wait()
											Cache:ReturnPart(Projectile)

										end)
										
									end
									
									OldPos = Projectile.Position
									
								end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking for some Raycast modules like FastCast and SecureCast, but they don’t work for the type of thing I’m trying to do, I’ve tried to take some examples and adapt them in my own way, but it never got to what I wanted.

can you send code with fixed indents, it is really hard to read

Event = RSE.Stepped:Connect(function(ElapsedTime, DeltaTime)

if (Projectile.Position - RootPart.Position).Magnitude > SkillTable.Distance then

if BV then 
BV:Destroy()
Direction = Vector3.new(0, -1, 0)
 end
end
									
local Cast : RaycastResult = workspace:Raycast(OldPos, -(OldPos - Projectile.Position).Unit * 999, Params)
									
if Cast and Cast.Distance < 5 then
										
print(Cast.Distance)
										
if Event then Event:Disconnect() end
if BV then BV:Destroy() end

Projectile.Anchored = true
Projectile.Position = Cast.Position

local Detection = HitModule:HitBox(Character, {
["Type"] = "Magnitude",
["Range"] = SkillTable.Range,
["CFrame"] = CFrame.new(Cast.Position),
})
										
local ShakeDetection = HitModule:HitBox(nil, {
["Type"] = "Magnitude",
["Range"] = 50,
["CFrame"] = CFrame.new(Cast.Position),
})

if ShakeDetection then
for TCharacter, Distance in pairs(ShakeDetection) do
local TPlayer = PYS:GetPlayerFromCharacter(TCharacter)
if TPlayer == nil then continue end

Remotes.Effects:FireClient(TPlayer, "ShakeOnce", {
["Duration"] = 5,
["Preset"] = "Explosion"
})
end
end

										Remotes.Effects:FireAllClients("GroundSmash", {
["Position"] = Cast.Position,
["Distance"] = 10,
["MaxRocks"] = 15,
["Size"] = Vector3.new(2, 2, 2),
["Cracked"] = true,
["Duration"] = 15
})

if Detection then
local Hitted = HitModule:HitApply(Character, Detection, SkillTable.HitTable)
if Hitted then
for TCharacter, Table in pairs(Hitted) do

local TRootPart = TCharacter:FindFirstChild("HumanoidRootPart")

Table["Fling"] and TRootPart then
Table["Fling"].Part = TRootPart
Table["Fling"].Direction = {Vector3.new(math.random(0, 25) / 100, 1, math.random(0, 25) / 100)}
MainModule:Velocity(Table["Fling"])
    end
   end
  end
 end
task.delay(15, function()
local Tween = TSE:Create(Projectile, TweenInfo.new(1, Enum.EasingStyle.Linear), {["Size"] = Vector3.new(0.001, 0.001, 0.001)})
Tween:Play()
Tween.Completed:Wait()
Cache:ReturnPart(Projectile)

 end)
end

OldPos = Projectile.Position

end)

Hello? I already fixed it, can you take a look and help me? I’m still stuck on this.

In case any of you have the same problem as me, what I did to fix it was basically this, I won’t give much information, just adapt it to your system.

You need Janitor and GoodSignal Modules.

How this work?

  • Do you need a Roblox speed modifier or do you make one of your own, changing the AssemblyLinearVelocity, the script will adapt to the speed, this contains some flaws, if I manage to fix them I probably won’t remember to update it from here so it will stay for yourself.

  • In my case I am using BodyVelocity along with this detection.

Example:

									local Detection = HitModule:HitBox(nil, {

										["Type"] = "Projectile",
										["Part"] = Projectile,
										["FilterDescendantsInstances"] = {workspace.Alive, workspace.Assets},
										["MaxDistance"] = SkillTable.Distance,

									})
									
									Detection.Cast:Connect(function(Result : RaycastResult)

		local Detection = {}
		Detection.__index = Detection
		local self = setmetatable({}, Detection)

		local OriginPos : Vector3 = Table["Part"].Position
		local Part : BasePart = Table["Part"]

		local Params : RaycastParams = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params.FilterDescendantsInstances = Table["FilterDescendantsInstances"]

		--

		self.Janitor = Janitor.new()
		self.Cast = self.Janitor:Add(Signal.new(), "Destroy")
		
		self.DeltaTime = 0.017
		self.ElapsedTime = 0
		
		self.Destroy = function() Detection = nil end
		
		task.spawn(function()

			while (Detection and Part and Part.Parent) and Part.Position.Y > workspace.FallenPartsDestroyHeight do
				
				self.Result = workspace:Raycast(Part.Position, (Part.AssemblyLinearVelocity + Vector3.new(0, -1, 0)) * (self.DeltaTime * 1.5), Params)
				self.Distance = (Part.Position - OriginPos).Magnitude

				--

				if self.Result then
					self.Cast:Fire(self.Result)

					if self.Janitor then
						self.Janitor:Destroy()
						self.Janitor = nil

					end

					break
				else
					
					self.ElapsedTime, self.DeltaTime = RSE.Stepped:Wait()
					
				end

			end

		end)

		return self

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