String.Sub issue

So I’m trying to get two parts of text from an IntValue.Name.

Explorer:
Error3

Each IntValue has a value of 12.

Code:

Error:

I am very unfamiliar with String.Sub, and it would be n0ice if I could receive some help!
Thanks! :smiley:

I’d use string.split if you’d like to get to different names from IntValue.Name by just writing down this:

local D = IntValue.Name:split(":") -- Change 'IntValue.Name' to your location of name from int value
print(D[1],D[2]) --Player2 Player1
1 Like

String.sub has 3 arguments, s, i, and j, according to the wiki. String.sub returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). Note that i and j have to be integers, not strings.

What you are doing in your variable “from” is setting j equal to a string, while in the variable “to” you’re setting i equal to a string as well. This is really easy fix though.

local from = string.sub(obj.Name, 1, #(string.find(obj.Name, ":")) - 1)
local to = string.sub(obj.Name, #(string.find(obj.Name, ":")) + 1)

Adding the “hashtag” as I like to call it (or the pound key), will simply count how many letters are in that string. This will turn it into an integer.

Also, what @ArticGamerTV said above, using string.split might make your life just a little easier.

2 Likes

@ArticGamerTV When I use your method, when I print the split name, it gives me “Value” and “nil”, instead of Player2, Player1. Any idea why?

Anteckning 2020-04-27 215734

EDIT: Nevermind, I just needed to wait for the instance to load in. Thanks for your reply and answer!

@Si_mple Yea, you were correct. String.Split made by life alot easier. Thanks for your help and answer!!

No problem! Have fun scripting! :slight_smile:

If you’re having an issue like this, then I suspect that you’re either using the parent argument of Instance.new or parenting before setting the value’s name. In this way, I assume the split is also being done immediately as an object of class IntValue gets added to the folder.

Never do that.

If you change the name before parenting, you won’t encounter such issues like needing to wait or anything of that sort. When an instance isn’t attached (parented) to the DataModel (game tree/hierarchy), you’re just modifying pieces of data. The moment it’s parented, it’ll already have its properties set, so you can immediately perform a split on the name.

2 Likes

@colbert2677 Ah, I did not know this. I guess you learn something new every day. I had actually parented it before setting the other stuff, I changed it around and now there’s no need for a wait. Thanks for the help!