How to use information from a StringValue in a LocalScript?

  1. What do you want to achieve? Keep it simple and clear!
    I want to get the value of a StringValue, (ex: Workspace.Music.Subfolder.Sound4), and use it within the script.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t figure out how to do that.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looked on the DevHub, and tried to use a loop with gmatch, but couldn’t get it to work.

The (local)script below is the one I’m trying to use. TRACKVal is the StringValue. What I’m trying to get to work is that the path from the StringValue is used as the curTrack. Then I can just do a curTrack:Play(). This is where I’m stuck. Thoughts?

local TRACKVal = script.Parent.Parent.Target
local playButton = script.Parent.play_arrow
local pausButton = script.Parent.pause
local TRACKTitle = script.Parent.Parent.Title

local curTrack = nil

playButton.MouseButton1Click:Connect(function()
	if playButton.Visible then
		pausButton.Visible = true
		playButton.Visible = false
		playButton.ZIndex = 1
		pausButton.ZIndex = 3
		print("Paused")

	elseif pausButton.Visible then
		pausButton.Visible = false
		playButton.Visible = true
		playButton.ZIndex = 3
		pausButton.ZIndex = 1
		print("Resumed")

	end
end)

pausButton.MouseButton1Click:Connect(function()
	if playButton.Visible then
		pausButton.Visible = true
		playButton.Visible = false
		playButton.ZIndex = 1
		pausButton.ZIndex = 3
		print("Paused")

	elseif pausButton.Visible then
		pausButton.Visible = false
		playButton.Visible = true
		playButton.ZIndex = 3
		pausButton.ZIndex = 1
		print("Resumed")

	end
end)
4 Likes

Help me understand, you want to convert the string into an object value basically?

1 Like

Yeah. So if the value of the StringValue is Workspace.Music.Subfolder.Sound5, I want to be able to take the value of the StringValue and use it as an object value. The StringValue.Value could then be used as if it were an object. Basically: curTrack:Play()

Alright, here’s how you can do that

local function ConvertString(str)

    local Object = script

    for _,v in pairs(str:split(".")) do
        Object = Object[v]
    end


    return Object
end

local Object = ConvertString(script.Parent.Parent.Target.Value)

I don’t really understand what you are exactly looking for but from what i understood here are your solution:

If the value of the StringValue is the song name, then you can do this (example)

local Sound = Workspace.Music.Subfolder:FindFirstChild(StringValue.Value)
Sound:Play()

Otherwise you can directly use an ObjectValue that index an Object as a value

ObjectValue.Value = Workspace.Music.Subfolder.Sound4
ObjectValue.Value:Play()
1 Like

Why don’t you just use ObjectValues? This lets you store the object directly and you wouldn’t have to do any shenanigans with it.

Well, the problem is that I have a selector panel. So basically someone can select a song, and the value of the song path is sent to the StringValue. It’s the only way I could think of passing values between scripts.

Yeah i see what you mean, but why you’re not indexing the sound Name or the sound Object in the value instead of the path ?

If you have any issues with the function try replacing the Object line to this

    local Object = game

and also start from the service instead in the string

There is an exemple of what you are looking to do, you just have to use an Object value.

Play/Pause script

local TRACKVal = script.Parent.Parent.Target
local playButton = script.Parent.play_arrow
local pausButton = script.Parent.pause

local curTrack = nil

local function ChangeButtons()
	if playButton.Visible then
		pausButton.Visible = true
		playButton.Visible = false
		playButton.ZIndex = 1
		pausButton.ZIndex = 3
		print("Paused")
		if curTrack then
			curTrack:Pause()
		end
	elseif pausButton.Visible then
		pausButton.Visible = false
		playButton.Visible = true
		playButton.ZIndex = 3
		pausButton.ZIndex = 1
		print("Resumed")
		if curTrack then
			curTrack:Play()
		end
	end
end

TRACKVal:GetPropertyChangedSignal("Value"):Connect(function()
	if curTrack == nil then
		curTrack = TRACKVal.Value
	else
		curTrack:Stop()
		curTrack = TRACKVal.Value
		curTrack:Play()
	end
end)

playButton.MouseButton1Click:Connect(ChangeButtons)
pausButton.MouseButton1Click:Connect(ChangeButtons)

Music selection script

local TRACKVal = script.Parent.Parent.Target
local Button = script.Parent
local Music = workspace.Music.Subfolder.Sound4

Button.MouseButton1Click:Connect(function()
	TRACKVal.Value = Music
end)

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