Is it possible to treat a variable like a string?

I want to make a tween that makes an object spin in circles, gradually getting faster. This is for particle effects for an attack. I have tried tweening bodyangularvelocity and a hingeconstraint. The constraint broke whenever it was being looked at by a player other than the one attacking, and the bodyangularvelocity only occasionally worked when turning the visual effects local.

So, is it possible to treat a variable like a string? I’d like to make a for loop that automatically makes a bunch of tweens. To clarify, I want to make the variable equivalent of doing “tween”…number to a string.

This is because tweening cframe angles seems to always try to go to the nearest neighbor so I have to increment the rotation slightly each time and make a new tween for it.

I’ve looked for how to do this in a bunch of times previously on the forum but I guess I don’t know how to word it properly or something like that, cause I’ve never found a way to do it.

Also, if you have a better way to do it other than tweening, let me know. Here’s the desired effect :
ezgif-1-6e0408cb8c62

1 Like

I don’t think I understand your particular use case but if you mean to turn a variable into a string you can use tostring(arg) or you can use the string concatenation operator.

No, my goal is to do the reverse of that. I need to create a bunch of variables with an increasing number at the end with a for loop.
So,
tween1 = (tween)
(loops)
tween2 = (tween)

You want to get the number in the variable?
example: variable1 you want to get the 1 and increase it ?

Yes, but in this case tween1 and tween2 have to be their own variables

Might want to use string patterns.

Screenshot_Chrome_20190815-135958

2 Likes

I don’t need to find something specific in a string, I need to turn a string into a variable. I can just put the variables in an array to reference them.

I assume you can use tables then, because then you can work with keys almost as strings using dictionarries.

local tweens = {tween1 = tween object, tween2 = tween object, ect..}
--and each time you wanna icrememnt the tween you can just do
tweens["tween"..tostring(the number you want)]

And there are deffintley better ways to do this instead of this but it’s good to try

Yes but that defeats some of the purpose. I think what I’m going to try to do instead is just to hook it up to an animationcontroller and make it into an animation which will probably work.

I don’t quite know what you are trying to get at here, maybe you can try to explain to me, but have you tried an iteration loop?

For Example:

for i = 1, 10, 1 do
 -- Code Here
end

You can use getfenv to technically do this, but what you are already trying to do is already bad practice.
If you need a specific number tied to a tween then the best case is to group them up in a table and have the tween reference the variable, increment the variable as needed.

I don’t really get what you’re asking here, but it looks like you should just use a Motor6D instead.

Ignoring the motor for now, from what I can understand, it sounds like you’re approaching this problem wrong. You should almost never be doing something like this:

Variable1 = 5
Variable2 = 7
Variable3 = 9

If you need to generate and store multiple variables, put them in a table.

Variable = {}
for i=1 , 10 do
   Variable[1] = 2 * i + 3
end

If using a table is somehow not possible, I think you may need to restructure your code.

1 Like

A variable is just a nickname for a value you have saved, that value can be a string (or anything else).
So if the variable is storing a string, then yes every time you treat the variable you will in fact be treating the string.
Not sure if this is what you’re actually asking.

If you want to have the variable name as a “String”, you can achieve some of that behavior through using a dictionary with the keynames as strings.

Ex:

local tb = {}
tb["tween1"] = whatever
tb["tween2"] = whatever2

for i = 1, numOfTweens do
    tb["tween" .. i]:Play() -- or something
end
2 Likes

Again, I was asking so I could just use a for loop to define them and avoid this exact scenario. But no matter, I ended up just making it an animation.

You should instead spawn an invisible and anchored Part and put a bunch of Attachments on it. Position your attachments in a way that would create your pattern. Then put your ParticleEmitters inside of those Attachments.

Then you can Tween the CFrame of the one Part however you want, and all the fires will move in unison.


If you still want to do tweens the way you want, you should cram those Tween objects into a table, like the others said, and then do a pairs loop on that table.

local tweens = {}
for i = 1, 50 do
    tweens[i] = game.TweenService:Create(SomeTweenInfoHere)
end
for i, tween in pairs(tweens) do
    tween:Play()
end

I like to parent my Tweens somewhere though, so I can find them like any other object in the game.


Yeah. I didn’t understand this well enough. Oops. I might retract this.

Yeah I already made the thing for the image, it wasn’t something I found online or anything, lol

The problem with the first approach is that since cframes round to the nearest neighbor you always have to have an increment for each tween which leads to crazy amounts of tweens to make it spin constantly, since you can’t have it loop without at least like, 80 tweens, which was why I was asking for how to define variables using a string for using a for loop to do it

If you like indexing tweens by strings, have you considered renaming and parenting them to the script that controls this effect? You can then say script.Tween1.

Here’s how it looks in the hierachy:

RobloxStudioBeta_2019-08-15_14-54-11

The actual attack is handled by the circle script, and to avoid bogging down the server with tweens the animate script animates it. The circle script clones animate into every player besides the one attacking so it gets replicated on everyone’s client, and the animate value is an objectvalue that points to the circle model so it knows what to animate.

Everything’s working fine right now, it’s doing it’s job pretty well. There might be a better way to handle replication, but this one seems pretty clean and easy to reuse.

You know, you could easily just make one tween for this. The CFrame rotation angle would be 360 * numberOfRotations, time would be some arbitrary plug, EasingDirection as out, EasingStyle as something that isn’t Linear, Back or Elastic.

When the angle is a number like this, it doesn’t use the shortest route to get to the number, it properly tweens up to the specified number.