Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using string to find object in Workspace?

I have a GUI that allows the player to insert a string value that the script will find that object in the workspace. For example the player inserts 'game.Workspace.Sound' in the TextBox then the script will find the Sound and do the script.

Error I'm getting

attempt to perform arithmetic (div) on nil and number

game["Run Service"].RenderStepped:Connect(function()
	while playing == true do
		wait()
	local sound = SoundLocation.Text -- TEXTBOX
	u1 = sound.PlaybackLoudness / 70; -- ERRORS HERE
	u2 = sound.PlaybackLoudness / 750;
	l__TweenService__4:Create(workspace.CurrentCamera, TweenInfo.new(u3:NextNumber(0.05, 0.1), Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
		CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(u3:NextNumber(-u1, u1)), math.rad(u3:NextNumber(-u1, u1)), math.rad(u3:NextNumber(-u1, u1)))
	}):Play();
	end
end);

1 comment

0

The game is reading it as u1 = SoundLocation.Text.PlaybackLoudness / 70.

2 answers

✓ Accepted answer
local text = "game.Workspace.Sound"

if text:sub(1,4):lower() == "game" then
	local directory = game
	if text:sub(6,14):lower() == "workspace" then
		local directory2 = directory:GetService("Workspace")
		if text:sub(16,20):lower() == "sound" then
			local finalDest = directory:FindFirstChild("Sound")
		end
	end
end

You could do something like this, because you can't use strings to coordinate the locations of an object in a game that is not equal to a variable.

You would just have to change it or find some kind of way to organize this into the way you want it.

Hope this helped!

2 comments

0

As you said I did have to tweak some things but I eventually got it from the help of you, Thank you.

0

No problem!

I think it's because you set sound to this variable

local sound = SoundLocation.Text

wich is finding "Text", and then you're doing

u1 = sound.PlaybackLoudness / 70;

but from what I know Text doesn't have a property named PlaybackLoudness, you should try doing

local sound = SoundLocation

so its finding SoundLocation, that has the property called PlaybackLoudness. I hope the problem is fixed!

6 comments

0

SoundLocation is a TextBox I

0

It submitted that for no reason... Anways, I'm trying to use the Text inside of the TextBox to find a Object within the workspace.

0

Alright, but PlaybackLoudness isn't a property of Text.

0

Yeah I know, I just don't know how to use a string to find an object in the Workspace.

0

I understood you already did it but were getting the error. So I made the script so you don't get the error anymore.

0

Although that's not what I'm trying to do.

This discussion is preserved as it was. New answers, comments, and votes are closed.