Water Part That will slow down you

I want to create part that will slow down player
I can’t make the player be in the part, he slowed down by 5 and when he left the part, he accelerated by 5 (the speed returned back)

This is my code. I tried to do this, but unsuccesful

local part = script.Parent

function SlowSpeed()
	part.Touched:Connect(function(hit)
	local PlrSpeed = hit.Parent.Humanoid.WalkSpeed
			print("Touched")
			PlrSpeed = PlrSpeed - 5
			wait(5)
	end)
end
function DefaultSpeed()
	part.TouchEnded:Connect(function(hit)
	local PlrSpeed = hit.Parent.Humanoid.WalkSpeed
			print("TouchEnded")
			PlrSpeed = PlrSpeed + 5
			wait(5)
	end)
end

script.Parent.Touched:Connect(SlowSpeed)
script.Parent.TouchEnded:Connect(DefaultSpeed)

i thought calling a function after touched already has the “hit” function

local part = script.Parent

function SlowSpeed(hit)
	local PlrSpeed = hit.Parent.Humanoid.WalkSpeed
	print("Touched")
	PlrSpeed = PlrSpeed - 5
    wait(5)
end
function DefaultSpeed(hit)
	local PlrSpeed = hit.Parent.Humanoid.WalkSpeed
	print("TouchEnded")
	PlrSpeed = PlrSpeed + 5
	wait(5)
end

script.Parent.Touched:Connect(SlowSpeed)
script.Parent.TouchEnded:Connect(DefaultSpeed)

I used “hit” to find player model in workspace that touched part, i don’t know if i did correct or not. I am new at scripting

Both TouchEnded and Touched are very buggy events, especially for moving parts such as the player.

I thought about it. But is there a replacement for these Events? i don’t know a lot

I recommend you to use this plugin Tag Editor to mark the parts with some tagName and then create LocalScript in StarterPlayerScripts with following code:

local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local SLOWDOWN_PARTS_TAG_NAME = "TEMPLATE"

local function incrementPlayerWalkSpeed(player: Player, speed: number)
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.WalkSpeed += speed
		end
	end
end

local function onSlowdownPartAdded(part: Instance)
	local ancestryChangedConnection: RBXScriptConnection = nil
	local affectedPlayers: { [Player]: boolean } = {}

	local heartbeatConnection = RunService.Heartbeat:Connect(function()
		-- additionally you could provide overlapParams instead of nil
		local baseParts = workspace:GetPartBoundsInBox(part.CFrame, part.Size, nil)
		local currentPlayers: { [Player]: boolean } = {}

		for _, basePart in ipairs(baseParts) do
			local player = Players:GetPlayerFromCharacter(basePart.Parent)
			if player then
				currentPlayers[player] = true

				if not affectedPlayers[player] then
					affectedPlayers[player] = true

					incrementPlayerWalkSpeed(player, -5)
				end
			end
		end

		for affectedPlayer in pairs(affectedPlayers) do
			if not currentPlayers[affectedPlayer] then
				affectedPlayers[affectedPlayer] = nil

				incrementPlayerWalkSpeed(affectedPlayer, 5)
			end
		end
	end)

	ancestryChangedConnection = part.AncestryChanged:Connect(function()
		if not part:IsDescendantOf(game) then
			ancestryChangedConnection:Disconnect()
			heartbeatConnection:Disconnect()
		end
	end)
end

for _, part in ipairs(CollectionService:GetTagged(SLOWDOWN_PARTS_TAG_NAME)) do
	task.spawn(onSlowdownPartAdded, part)
end

CollectionService:GetInstanceAddedSignal(SLOWDOWN_PARTS_TAG_NAME):Connect(onSlowdownPartAdded)
1 Like

Best script much optimized :wink: Give Solution

local part = script.Parent
local bool = false
local Character
local SlowSpeed=5
local NormalSpeed=12
part.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		bool=true
		Character = part.Parent
	end

end)
part.TouchEnded:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		bool=false
	end
	
end)

local adjusted = false

while wait() do
	if Character then
	if Character.Humanoid.Health>0 then
		
		local xparts = Character.HumanoidRootPart:GetTouchingParts()
		
		
		
		if adjusted==false then
			for i,v in pairs (xparts) do
				if v==part then
					adjusted = true
					print("X")
				end
			end
			
			if adjusted== true then
				Character.Humanoid.WalkSpeed=SlowSpeed
			else if bool==true then
					Character.Humanoid.WalkSpeed=SlowSpeed
				else
					Character.Humanoid.WalkSpeed=NormalSpeed
				end
				
				
			
			
			end
			
		end
		
		
	end
	end
end


1 Like

robloxapp-20220216-2208272.wmv (1.9 MB)

1 Like

Thanks for recommendation, i will use it in future!

personally i change the script so a local script detect touched parts of your humanoidrootpart instead of making it server heavy load but it still will work fine

When humanoid root part touching part it’s printing X in output and player becomes slow all the time, after reset slowing down doesn’t work.

did you put the script in a part in the workspace?

Yes i did

did you change the values at the top its set to 12 not 16

this would be normal default roblox speed

It does not help. Part that touched Humanoid RootPart breaks, part that is not touched RootPart works fine

How does it break? Can you not move or what?

local part = script.Parent
local bool = false
local Character
local SlowSpeed=5
local NormalSpeed=16
part.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		bool=true
		Character = part.Parent
	end

end)
part.TouchEnded:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		bool=false
	end
	
end)

local adjusted = false

while wait() do
	if Character then
	if Character.Humanoid.Health>0 then
		
		local xparts = Character.HumanoidRootPart:GetTouchingParts()
		
		
		
		if adjusted==false then
			for i,v in pairs (xparts) do
				if v==part then
					adjusted = true
					print("X")
				end
			end
			
			if adjusted== true then
				Character.Humanoid.WalkSpeed=SlowSpeed
			else if bool==true then
					Character.Humanoid.WalkSpeed=SlowSpeed
				else
					Character.Humanoid.WalkSpeed=NormalSpeed
adjusted = false
				end
				
				
			
			
			end
			
		end
		
		
	end
	end
end


When you going out of part that touched Humanoid RootPart it’s not bring back speed to normal

local water = script.Parent

water.Touched:Connect(function(partStart)
	local model = partStart:FindFirstAncestorOfClass("Model")
	if model then
		local human = model:FindFirstChildOfClass("Humanoid")
		if human then
			human.WalkSpeed = 5
			local connection do
				connection = water.TouchEnded:Connect(function(partEnd)
					if partStart == partEnd then
						human.WalkSpeed = 12
						connection:Disconnect()
					end
				end)
			end
		end
	end
end)
1 Like

@Sedny157 your script is good, but change the argument after equals from an equation to an exact amount.

The default roblox speed is 16

So what you should do i make it 11, and on touchended, make it back to 16

1 Like