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

why isnt print printing the name?

local player = game.Players.LocalPlayer
local name = script.Parent.TextButton.Text
script.Parent.TextButton.MouseButton1Down:connect(function()
game.Players.LocalPlayer.Backpack:FindFirstChild(name)
	
	print(name)
		
	end)
		

there's no error, it prints something, but it's just a blank line. and there is a weapon name there.

i take it im doing something wrong.

what im trying to do is find the name of the weapon in the text button and equip that weapon

now output is printing

17:20:30.651 - Players.Fearme987.PlayerGui.Main.Holder.1.Script:6: attempt to call method 'Clone' (a nil value)

from this

local player = game.Players.LocalPlayer
local name = script.Parent.TextButton
script.Parent.TextButton.MouseButton1Down:connect(function()
local tool = player.Backpack:FindFirstChild(name.Text)

name.Text:Clone().Parent= player.Backpack
	
	print(name.Text)
		
	end)

1 answer

✓ Accepted answer

Problem #1:

I assume you are trying to 'search' for a weapon inside the player's Backpack. This cannot be done if name is not a userdata (AKA, object).

When you set a variable, it has no room to change, meaning that TextButton.Text will not change the variable when Text is changed. This means you have to use the userdata instead;

local name = script.Parent.TextButton -- leaving room for change
print(name.Text)

Problem #2:

Some of your formatting is.. flawed. That makes it a bit hard to understand, and I actually got confused between lines 3-4.

As well as that, use Tab to format your code, since Space kinda sucks.

A simple fix:

local player = game.Players.LocalPlayer
local name = script.Parent.TextButton

script.Parent.TextButton.MouseButton1Down:connect(function()
	game.Players.LocalPlayer.Backpack:FindFirstChild(name.Text)
	print(name.Text)
end)

Problem #3:

By now, your code should be able to print the name (see previous problem's code block). The problem next is that you aren't doing anything with the weapon you are searching for, since FindFirstChild() returns a userdata (the weapon you found).


To equip it:

local player = game.Players.LocalPlayer
local name = script.Parent.TextButton

script.Parent.TextButton.MouseButton1Down:connect(function()
	local obj = game.Players.LocalPlayer.Backpack:FindFirstChild(name.Text)
	if obj then -- if the weapon exists
		obj.Parent = player.Character -- equips it
		print(name.Text)
	end
end)

Problem #4:

There is a chance that your code will error in the server because script.Parent.TextButton may not exist yet. So:

local name = script.Parent:WaitForChild("TextButton")

Hope I helped!

~TDP

1 comment

0

dude you're awesome :)

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