Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does this function never run when the part is touched?

Asked by
udoxas 75
8 years ago

Alright. I'm trying to make a block that slows you down while you're inside it, but gives you normal speed when you leave it. But for some reason, after making it so your walkspeed doesn't go infinitely high, it doesn't work at all. I added a print every other line starting in the functions, and it doesn't get past the line 5 print for some reason. I need this to work with custom speeds (which is why it uses division and multiplication) so don't suggest making the numbers fixed.

touched = false

function touched(hit)
    if hit.Parent:FindFirstChild("Humanoid") and touched == false then
        print("line 5")
        touched = true
        print("line 7")
        hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed / 2
        print("line 9")
    end
end

function touchended(hit)
    if hit.Parent:FindFirstChild("Humanoid") and touched == true then
        print("line15")
        hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed * 2
        print("line17")
        touched = false
        print("line19")
    end
end

script.Parent.Touched:connect(touched)
script.Parent.TouchEnded:connect(touchended)

I literally have no idea what's wrong with this and I am extremely confused and frustrated.

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago
local touched = false

function touched2(hit)
    if hit.Parent:FindFirstChild("Humanoid") and touched == false then
        print("line 5")
        touched = true
        print("line 7")
        hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed / 2
        print("line 9")
    end
end

function touchended(hit)
    if hit.Parent:FindFirstChild("Humanoid") and touched == true then
        print("line15")
        hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed * 2
        print("line17")
        touched = false
        print("line19")
    end
end

script.Parent.Touched:connect(touched2)

script.Parent.TouchEnded:connect(touchended)

this worked for me. i changed the

function(touched)
script.Parent.Touched:connect(touched)

to

function(touched2)
script.Parent.Touched:connect(touched2)

it was connecting to the bool Value for some weird reason

Output:

line 5
line 7
line 9
line15
line17
line19
line 5
line 7
line 9
line15
line17
line19
line 5
line 7
line 9

it prints the touch ended prints on it while im walking on the brick in my testing server also, the WalkSpeed keeps changing from 8 to 16 while walking. it doesn't look like it speeds up though

0
Works fine for me after changing the function name. Thanks for telling me it was connecting to the variable - I thought it would know which one was the variable and which one was the function. udoxas 75 — 8y
0
i thought so too. but we both learned something today RobloxianDestory 262 — 8y
0
Good job. Was gonna solve it myself, but looks like someone beat me to it.:) Meltdown81 309 — 8y
Ad

Answer this question