How to convert a string to a variable?

So I am trying to get some sound effects to work, but I am having a hard time getting it to use the variable instaid of the string.

    local Fists = Instance.new("Sound", KillBox)
	Fists.SoundId = "rbxassetid://8646342913" 
	Fists.EmitterSize = 10
	Fists.Parent = KillBox
	local Knife = Instance.new("Sound", KillBox)
	Knife.SoundId = "rbxassetid://220833976"
	Knife.EmitterSize = 10
	Knife.Parent = KillBox

    local attack = script:GetAttribute("Weapon")
    local attackID = script:GetAttribute("SoundID")

	attack:Play(attackID)

The “attackID” works fine because it’s just a number, but “attack” needs to be a variable, and it is currently just a string. I have been tinkering around and searching for answers online, but I cant find something that works for this.

What is an example of the string that is stored in the Weapon attribute?
What is the position of the sounds in explorer? (Send a screenshot of the explorer where the sound files are visible, with their path also being visible)

Currently the only weapons I have made for it are “Fists” and “Knife”. Both of which are strings.

Here is the sound location:

The sounds are inside a “Killbox”, which is inside a player model. Is the problem that all the sounds have the same name? I never had that problem before.

The sounds will need to have unique names, yeah.

local Fists = Instance.new("Sound", KillBox)
Fists.SoundId = "rbxassetid://8646342913" 
Fists.EmitterSize = 10
Fists.Parent = KillBox
Fists.Name = "Fists"

local Knife = Instance.new("Sound", KillBox)
Knife.SoundId = "rbxassetid://220833976"
Knife.EmitterSize = 10
Knife.Parent = KillBox
Knife.Name = "Knife"

local attack = script:GetAttribute("Weapon")
local attackID = script:GetAttribute("SoundID")

KillBox:FindFirstChild(attack):Play(attackID)

Thanks! I never thought about using “FindFirstChild”.

1 Like

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