Can't figure out something while scripting

HI, I have been trying to make it so my custom tag has a new random tag with random characters in between the “[” and the “]” every seconds like in the screenshot


I have managed to generate a random string and added the

"["

but can’t figure out on how to add the second

"]"

. I have a reference on how it should look in the screenshot.

local Players = game:GetService("Players")
local HidePart = script.Parent

HidePart.Touched:Connect(function(Character)
	local Player = Players:GetPlayerFromCharacter(Character.Parent)
	      if Player.Name == ("UnnamedSkilI") then
			local guy = game.Workspace:FindFirstChild(Player.Name)
			local Name = guy.Head.Rank.User
			local Rank = guy.Head.Rank.Team
		
		while true do 
			for i = 0,6,1 do
				Name.Text = Name.Text .. string.char(math.random(33,126)) -- The generated text string
			end
			wait(1)
			Name.Text = "[ " -- Adding the first one
		end
		script.Enabled = false
		wait(60)
		script.Enabled = true
		Rank:Destroy()
	else

	end
end)

I have tried many things but can’t still do it, If anyone could help me I would really appreciate it.
If you also have some feedback on how to optimize the script that would be cool!

Have a great day

Try this for your while true do loop.

while true do 
		for i = 0,6,1 do
				Name.Text = "[ " .. string.char(math.random(33,126)) .. " ]"
		end
		wait(1)
	end
1 Like

Hello, this is adding the

"["

but not the

"]"

I would declare a variable and then just store the random string in the variable so instead do for example randomString = randomString … string.char(math.random(33,126))

and then when you have the length you want do Name.Text = “[ " … randomString … " ]”

1 Like

also i wouldnt do a while true loop, for example if you want it to change every minute do while wait(60) do

1 Like

Hello, This is still adding the

“[”

But not the

“]”

Maybe I’m doing something wrong here is what i did

		while wait(1) do
			for i = 0,6,1 do
				Text = Text .. string.char(math.random(33,126))
			end
			Name.Text = "[ " .. Text .. " ]"
		end

There are Multiple ways to this:

  1. concatenation
    You can add onto a string by using .. to another item, like for Example:
local currentString = "[ "..otherstring.. " ]"
-- your strings will get added together to form a single string.
  1. string.format()
    You are able to format your strings using a specific specifier, this this case, you can use %s which is used for strings.
local currentString = string.format("[ %s ]", otherstring) -- format
-- This will give you the format of your stringm %s would be replaced with 'otherstring'

-- Alternate Version:
local currentString = ("[ %s ]"):format(otherstring) -- format

Should also be using a seperate variable instead of using the same one, so like this:

local otherstring -- Empty variable, Defaults to nil
for i = 1,6 do -- this will run 6 times instead of 7
    otherstring = Text .. string.char(math.random(33,126)) -- Applies Data to Outside Variable
end
1 Like

Try printing the Name.Text out and see if it returns it with both brackets or not. Maybe it is correctly displayed in the Text in the Label and it’s just a display issue. In your picture I just noticed there also seems to be no space between the bracket, which you added that in your script, and the random string so maybe its too tiny for the string?

1 Like

Hello!! This does in fact work but I only get one character instead of 6 (Screenshot)
image
Here is what i did:

while wait(1) do
	local otherstring
		for i = 1,6 do 
			otherstring = string.char(math.random(33,126)) 
		end
		local currentString = ("[ %s ]"):format(otherstring)
		Name.Text = currentString
	end
	script.Enabled = false
	wait(60)
	script.Enabled = true
	Rank:Destroy()

I did a bit of Testing, its this thats the Problem:

otherstring = string.char(math.random(33,126))

You Removed Text from the code I provided, so it should be:

otherstring = Text .. string.char(math.random(33,126))

Edit: Actually nevermind, its actually this:

otherstring = otherstring .. string.char(math.random(33,126))

You should then have otherstring as an empty string which is just "" so there is no errors with "Attempt to concat string with nil"

local otherstring = ""
for i = 1,6 do 
	otherstring = otherstring..string.char(math.random(33,126)) 
end
1 Like

Hello now it just doesn’t works here is the error i’m getting:

Workspace.Folder.CODENAME.HideName.ChangeName:14: attempt to concatenate nil with string  -  Server - ChangeName:14
  00:10:12.870  Stack Begin  -  Studio
  00:10:12.870  Script 'Workspace.Folder.CODENAME.HideName.ChangeName', Line 14  -  Studio - ChangeName:14
  00:10:12.870  Stack End  -  Studio

Here is the script

while wait(1) do
	local otherstring
		for i = 1,6 do 
			otherstring = otherstring .. string.char(math.random(33,126))
		end
		local currentString = ("[ %s ]"):format(otherstring)
		Name.Text = currentString
end

I basically just gave you th fix to it.

2 Likes

Sorry i didn’t saw it, thanks for helping me fix it now it’s working!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.