Help with script

How to make when the player touches a block it slows it down and when it stops touching it returns it to its normal speed?

do you mean slowing down the player?

You can do something like:

script.Parent.Touched:Connect(function(hit) -- Gets the toucher
   local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid") --Checks if hit is a character (checks if humanoid is a child of hit)
   if hum then -- if hum is found then
   repeat wait() until script.Parent:GetTouchingParts() == nil -- repeats until no parts are touching the script.Parent
    hum.WalkSpeed = 50
   else
   hum.WalkSpeed = 16
   end
end)

OOPS, edited, had extra ends

2 Likes

Touched/TouchEnded Events

You could probably use Magnitude for this, and frequently looping through every Player you can check if they’re near/touching the Block or not to give them speed

I was gonna say Region3, but it would probably result in too much confusion

Just loop through all the players using a nested loop, and check if they’re touching the part or not:

local Part = workspace.Part

wait(5)

while true do
    print("This is looping fine")
    for _, Player in pairs(game.Players:GetPlayers()) do

        local Character = Player.Character
        local HRP = Character:WaitForChild("HumanoidRootPart")

        if (Character.HumanoidRootPart.Position - Part.Position).Magnitude < 10 then
            print(Player.Name.." is in the proximity of the part")
            Character.Humanoid.WalkSpeed = 5
        else
            print(Player.Name.." is not in the proximity")
            Character.Humanoid.WalkSpeed = 16
        end
    end
    wait(.1)
end

@Winbloo Your script would error because hum would either be checking for the actual Humanoid Instance, or it’ll return back as a nil value so if this line here starts to run:

hum would be defined as nil

1 Like

Hmm, didn’t I set hum’s variable?

You did, but sometimes hit.Parent may not be able to detect a valid Humanoid Instance

You’re assuming that hum already has the Humanoid's properties in your else statement, which could result as an error of some sort

script.Parent.Touched:Connect(function(Hit)
    local Hum = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

    if Hum then --We are checking for the Humanoid if it's valid or not

    else --This would be if there's no Humanoid found, which would result as a "nil" value and therefore error
end)
1 Like

Ooh, right. I should use a elseif instead, sorry about that :sweat_smile:

1 Like

It does not work, what I can do?

You could try increasing the Size detection of the Magnitude? Experiment with it a little bit & see what results you get

1 Like