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

Why is script falsely renaming a tool?

Asked by 4 years ago

Ovations, everyone. I was writing a script, where it renames your tool when you touch something. My tool is called Mop and it is still renaming it to Mop | 3/3 .. What is the problem with my script? I would be glad if anyone could help me.

script.Parent.Touched:connect(function(h)
 if h.Parent.Name == "Mop" then 
        h.Parent.Name = "Mop | 1/3"
    elseif h.Parent.Name == "Mop | 1/3" then 
        h.Parent.Name = "Mop | 2/3"
    elseif h.Parent.Name == "Mop | 2/3" then 
        h.Parent.Name = "Mop | 3/3"
end
end)

Every single help is appreciated, thank you!

1 answer

Log in to vote
1
Answered by
zadobyte 692 Moderation Voter
4 years ago

Since .Touched events fire pretty fast youll need a debounce and TouchEnded.

local deb
script.Parent.Touched:connect(function(h)
    if not deb then
        deb = true
        if h.Parent.Name == "Mop" then 
                h.Parent.Name = "Mop | 1/3"
            elseif h.Parent.Name == "Mop | 1/3" then 
                h.Parent.Name = "Mop | 2/3"
            elseif h.Parent.Name == "Mop | 2/3" then 
                h.Parent.Name = "Mop | 3/3"
        end
    end
end)
script.Parent.TouchEnded:Connect(function(h)
    if string.match(h.Parent.Name, "^Mop") then --checks if the tool's name starts with mop
        deb = false
    end
end)
0
It still names it "Mop | 3/3" when it should call it "Mop | 1/3" as the tool is called "Mop". Your answer is appreciated tho! ggAmazingMan 32 — 4y
0
have you tried placing a print in .Touched and .TouchEnded? zadobyte 692 — 4y
0
Yep, they both printed. It gotta be an issue with the renaming. ggAmazingMan 32 — 4y
0
how often did they print zadobyte 692 — 4y
0
Really often, I think I found the problem btw. It goes trough every step so fast that I cannot even see it. Like, it renames it to 1/3 but then it renames it to 2/3 so fast that I cannot see it. ggAmazingMan 32 — 4y
Ad

Answer this question