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

String.sub script not working?

Asked by
JJ_B 250 Moderation Voter
8 years ago

I made a script that kills someone if their name is not the same as a model's name (after 2 letters,) however, the script continues to kill everyone, regardless of their name. There are no errors. Here is the script:

script.Parent.Touched:connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then
        script.Parent.hit:Play()
        hit:Destroy()
    else
        local nm = hit.Parent.Parent.Name
        local tb = script.Parent.Parent.Name
        if nm ~= string.sub(tb,3) then
        hit.Parent.Humanoid:TakeDamage(100)
        script.Parent.hit:Play()
        wait(1)
    end
    end
end)

1 answer

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

According to the documentation of string.sub:

The call string.sub(s,i,j) extracts a piece of the string s, from the i-th to the j-th character inclusive.

You called it as string.sub(tb,3), which would return a string composed of the third character and everything after it. To get only the third character, you would have to do string.sub(tb, 3, 3).

note: I noticed that you edited it from tb:sub(3) to string.sub(tb, 3). The first way also works.

Ad

Answer this question