How to print StringValue

Hello everyone, I wanted to print two separated words in a StringValue, how would I go about doing this? My current code doesn’t work and only manages to print ‘sentence’.

local laws = script.Parent.Laws

for _, v in laws:GetChildren() do
	local clone = script.Parent.Laws.LawNameTemplate:Clone()
	
	if v:IsA("Folder") then
		print(v:GetChildren(name, sentence))
		name = v.Name.Value
		sentence = v.Time.Value
		
		print(name, sentence)
	end
end```
1 Like

Name is a property of the parent instance which is inherited from the Instance base class. Luau will check for the property before checking for the child with the name, so it finds the field Name before it even searches for the values, which is giving the error (presumable) “Attempt to index string with ‘Value’”.

Rename the value to fix this.

If these are literally two words, and one is not a sentence like the name suggests, try using one value with string.split, or even better, an attribute.

3 Likes

An alternative is to use functions specifically looking for children, such as FindFirstChild:

name = v:FindFirstChild("Name").Value

Although yeah, using common properties as instance names is a bad practice.

1 Like

Yeah, you could also use FindFirstChild, but it’s kind of a never ending debate, I guess.

I heard that it’s proven that indexing a child is ~20% faster than FindFirstChild, but of course you need to know the child exists and it messes with the typechecker, as seen if using strict mode. But FindFirstChild is made for finding instances and won’t error if the instance doesn’t exist, and also has a recursive option (not relevant here really).

I mean, both our solutions would work. It’s down to preference I guess :man_shrugging:.

(I indicated indexing here to keep it relevant to the OP’s original code.)

2 Likes

Did you consider using: FastValue a replacement for Value Instances that is better than Remotes and Bindables to also create tables?