Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer
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

3 comments

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.

0

i thought so too. but we both learned something today

0

Good job. Was gonna solve it myself, but looks like someone beat me to it.:)

This discussion is preserved as it was. New answers, comments, and votes are closed.