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 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)
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
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
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
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)