Scripting A slow part

How could i go about making a part that slows down the player when they are touching it, any time i have tried to script it the player can just counter it by spam jumping.

1 Like

Do you want it so that they touch it once and they slow down or that as long as they touch it their speed is reduced?

1 Like

You could simply add a hitbox to the part, so if the player jumps they are still touching it.

1 Like

as long as they touch it as well to that hitbox i have it where the parkour over it kinda like the floor is lava

I have a question: Is this handled on the server or client?

currently the server as the script is in the part

If the player slows down while over the part, then you could use raycasting.

local part = -- your part --
local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		RunService.Heartbeat:Connect(function()
			local raycastParams = RaycastParams.new()
			
			local origin = part.Position
			local direction = Vector3.new(0, *prefered Y*, 0)
			local result = workspace:Raycast(origin, direction, rayParams)

			if result.Instance == HRP then
				hum.WalkSpeed = -- Walkspeed you want to set
			else
				hum.WalkSpeed = -- Whatever walkspeed you have the default set to
			end
			
		end)
	end)
end)

I’m not too experienced with scripting, but I tried. I hope this helps!

1 Like

i was able to do it like this not 100% bug tested

local speedPart = script.Parent

-- Stores active timers for each character to manage resets.
local activeTimers = {}

-- Handles the part's touch event.
local function onTouch(otherPart)
	-- Get the character model from the part that was touched.
	local character = otherPart.Parent

	-- Find the Humanoid to confirm it's a player or NPC.
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		-- For debugging.
		print(character.Name .. " touched the speed part. Setting speed to 10.")

		-- Slow the player down.
		humanoid.WalkSpeed = 10

		-- If a timer already exists for this player, cancel it to reset the countdown.
		if activeTimers[character] then
			print("An existing timer was found for " .. character.Name .. ". Resetting the 2-second countdown.")
			task.cancel(activeTimers[character])
		end

		-- Start a new 2-second timer to reset the speed.
		activeTimers[character] = task.delay(2, function()
			-- Make sure the player still exists before resetting their speed.
			if character and humanoid then
				print("Timer finished for " .. character.Name .. ". Resetting speed to 30.")

				-- Reset speed to normal.
				humanoid.WalkSpeed = 30
			end

			-- Remove the completed timer.
			activeTimers[character] = nil
		end)
	end
end

-- Connect the function to the Touched event.
speedPart.Touched:Connect(onTouch)

That’s great!

1 Like

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