Hello im trying to make a script that checks if a player is on the ground by using a raycast, but I’m having some trouble. Everytime I jump in the air there are a few instances where it detects it hit something. how do I fix this? this is my code:
local player = game.Players.LocalPlayer -- Reference to the player
local character = player.Character or player.CharacterAdded:Wait() -- Get the player's character
character:WaitForChild("Humanoid").JumpHeight = 100
--params
local raycastParams = RaycastParams.new(character)
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
-- Function to check if the player is on the ground
local function isPlayerOnGround()
local origin = character.PrimaryPart.Position -- Starting position for the ray
local direction = Vector3.new(0, -1, 0).Unit -- Downward direction
-- Perform raycast
local result = workspace:Raycast(origin, direction, raycastParams)
return result
end
while true do
task.wait()
-- Example usage
local raycastResult = isPlayerOnGround()
if raycastResult then
print("Player is on the ground")
print(raycastResult.Position)
else
print("Player is in the air")
end
end