Hi im trying to use a raycast to detect when the player is on the ground so i can code in some features in future but after a bit of the code running there is extreme slowdown and i dont know how to fix it
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0,-1,0)
game:GetService("RunService").Heartbeat:Connect(function()
local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
if rcresult then
local hp = rcresult.Instance
if hp then
print("onground")
end
if not hp then
print("offground")
end
end
end)
i tried using Destroy() on the rays but that isnt a property of them :\
Instead of casting a ray every frame, try casting one only when the character’s RootPart changes. And to further optimize, you could set up a debounce.
do
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0,-1,0)
local enabled = true
yourCharacter.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
if enabled then
enabled = false
local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
if rcresult then
local hp = rcresult.Instance
if hp then
print("onground")
else
print("offground")
end
end
task.wait(1) --// There may be a better way to do this, but this would work
enabled = true
end
end)
end
You’re right, I just decided to make it 1 to show that you should put a number in between, otherwise it would be the same as using heartbeat since task.wait() runs using heartbeat
I have not. You may be right as I don’t use GetPropertyChangedSignal often enough to know, but it would still be a better practice to only raycast when the character moves if possible.
try your original code but put an if statement that checks if the last position of the rootpart is 0.2 more magnitude than the current position. this is a good substitute for GetPropertyChangedSignal(“Position”)
Ok i think i got it to work how i like thank you! here is the code:
while wait() do
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0,-1,0)
local enabled = true
local currentpos = Character.HumanoidRootPart.Position.Magnitude
wait(.2)
local lastpos = currentpos
wait()
local currentpos = Character.HumanoidRootPart.Position.Magnitude
if currentpos - lastpos >= 0.2 or currentpos - lastpos <= -0.2 then
if enabled then
enabled = false
local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
if rcresult then
local hp = rcresult.Instance
if hp then
print("onground")
else
print("offground")
end
end
task.wait(.1)
enabled = true
end
end
end
Using delays and position thresholds is bad for this use case. You want your game to respond instantly when your character leaves the ground, right? Those optimizations will break that.
The optimizations are also unnecessary. You are not slowing down your game by casting one 1-length ray every frame. Even if you were, casting it after x time wouldn’t help because you’d still be excessively slow whenever it casts.
The prints are the culprit of any timing issues in the code you posted. Printing can take a relatively long time, and when you do it every frame you can really slow down your game. Remove them and see how your game performs.
Oh damn thanks for that! i had no clue that print was so performance heavy, I had a problem though with all the scripts being that it wont print the “offground” stuff and it would only just stop the onground print from printing so, you would see that your off the ground by the delay in the next onground print but offground wont actually print