My string value isn't printing!

I was going to debug my script real quick with the print function. My script was supposed to get a StringValue’s value, and use that value to look for a child that has the according value as its name. After finding the child, it will print the child’s name.

After I tried to execute it, it gives me an error the line of getting the variable of the child. So I put a if [Variable] then line, and put a warn function if it doesn’t exist. And it doesn’t exist.image
(The local function is working fine btw)

I tried using WaitForChild, FindFirstChild. But that’s all there is for me to think of.

local function StandPunched(player)
	local Character = player.Character		
	local Stand = player.Backpack:WaitForChild(player.StandValue.Value)
	if Stand then
		print(Stand.Name)
	else
		warn("dum dum")
	end
end

game.ReplicatedStorage.StandPunch.OnServerEvent:Connect(StandPunched)

2 Likes

Make “Stand” an object variable (so remove the .Value), and print Stand.Value.

WaitForChild() searches for an object that matches the String argument. If you’re waiting for player.StandValue, then WaitForChild() would search for nil.

local Stand = player.Backpack:WaitForChild("StandValue") -- get the object

print(Stand.Value) -- print the value of the object

Also, your code looks like the StandValue would already be inside the player when the function is called. If so, it would be better to use :FindFirstChild(“StandValue”)

Check what the Value of StandValue is.

That’s not how :WaitForChild() works. Two things:

  1. You can’t pass objects in the :WaitForChild().
  2. Lets keep the first one aside. You are trying to wait for StandValue.Value, which is a property, not a child.

Solution:

  1. You need to pass a string with the full name of what you want to wait for. In your case:
local Stand  = player:WaitForChild("StandValue")
  1. You access to its value by simply doing:
Stand.Value

Also, Why are you doing this:

if Stand then

Doing this is of no use, as :WaitForChild() either gets the child, or errors, breaking the script.

As @MayorGnarwhal mentioned already, you can use :FindFirstChild(), as it either gets the child, or returns nil, without breaking the script.

2 Likes

I had to print the name of the child in the player’s backpack to make sure the script is able to configure around with the child. Not the StandValue’s value.