Sliding with raycasting

I have tried to make a sliding system with raycasting but there’s a few problems with it, I need a way to constantly detect what part the player is hitting so that the tilt/animations etc can stop, I’ve used run service but I realised it doesn’t constantly update what part you are under, only when you press CTRL (the keybind to slide)

The parts the player can slide on are named “CanSlide”

(https://gyazo.com/218980e3922bf55d658ced944796f218)

Interesting topic. Are you sure run service is firing constantly or is it just using RenderStepped:Wait()?

Sounds like you are connecting a function to the wrong event. Can you provide code samples?

script.Parent.OnServerEvent:Connect(function(Player,on)
	Player.Character.Sliding.Value = on
	slidesound.Parent = Character.HumanoidRootPart
	slidesound:Play()
	slideanim:Play()
	local ray = Ray.new(player.Character.HumanoidRootPart.CFrame.p, Vector3.new(0,-10,0), params)
	if on then
		tiltFunction = game["Run Service"].Heartbeat:Connect(function()
			local hit, position, normal = workspace:FindPartOnRay(ray, game.Workspace.Alive)
			if hit and hit.Anchored == true and hit.CanCollide == true and hit.Transparency ~= 1  and hit.Name == "CanSlide" then

I’m using heartbeat, since I’ve never known the difference between both

I don’t see a single instance of Heartbeat in the code sample. Also, I think you should be doing this all on the client.

tiltFunction = game["Run Service"].Heartbeat:Connect(function()

It’s in a server script

image

That is what I’m worried about. Do not fire heartbeat events to the client. It will lag the game.

You should use Raycast, your implementation has been deprecated. You should do this on the client as well. Using run service’s stepped puts you closer to the physics engine.

local raycast_params = RaycastParams.new()
raycast_params.FilterType = Enum.RaycastFilterType.Blacklist
raycast_params.FilterDescendantsInstances = {player_character}

local run_service = game:GetService("RunService")
start_slide_event.Event:Connect(function()

    local connection
    connection = run_service.Stepped:Connect(function()
        local raycastResult = workspace:Raycast(player_character.HumanoidRootPart.Position, Vector3.new(0, -10, 0), raycast_params)

        if raycastResult then
            if raycastResult.Instance.Name ~= "CanSlide" then
                print("Hit not slidable!")
                connection:Disconnect()
            else
                print("Still good!")
            end
        else
            print("Nothing was hit!")
            connection:Disconnect()
        end
    end)
end)
2 Likes

But I want it to show to other people as well

I am pretty sure you can play animation on the character from the client. If not it would still be much better to use this script on the client and fire a remote when it starts and ends to animate.

It’s works better now, but everytime the player is floating for a slight second or so there’s major lag

nvm it was a repeat loop it’s not laggy anymore

To be on the physics engine, use RunService.Heartbeat, which can be used on the client and server.

It might lag though.