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

How to find object using it's name?

I'm working on a shop system for my game and I've already got most of the system done, but I've added boolvalues in ReplicatedStorage, so that when it sends data to the main game, you'll will spawn will those items you purchased. I've already scripted it, but it does nothing. It's supposed to find the boolvalue in ReplicatedStorage by using the tower name you just purchased.

Here's the script. Thanks a lot, notreallysupernova.

local coin = workspace.Coin
local errorSoundEffect = workspace.Error
local Price = script.Parent.Parent.TowerPrice
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	if player.leaderstats:FindFirstChild("Coins").Value >= Price.Value then
		-- player.leaderstats:FindFirstChild("Coins").Value = player.leaderstats:FindFirstChild("Coins").Value - Price.Value
		coin:Play()
		-- Here is where I tried to write where tries to find a object in ReplicatedStorage by it's name.
		for _, Tower in pairs(game.ReplicatedStorage.OwnedTowers:GetChildren()) do
			if Tower.Name == script.Parent.Parent.TowerName then
				Tower.Value = true
				print(Tower.Value)
			end
		end
	else
				errorSoundEffect:Play()
	end
end)

2 answers

Try this.

game.ReplicatedStorage:FindFirstChild(script.Parent.Parent.TowerName).Value = true

This makes the replicated storage search for the item with the same name as the tower. If that doesn't work then put

game.ReplicatedStorage:FindFirstChild(script.Parent.Parent.TowerName.Value).Value = true

1 comment

0

It doesn't work. It gives an error saying "attempt to index nil 'value'"

Putting "true" as the last argument value in findFirstChild makes it go through all the things in ReplicatedStorage. Try this:

game.ReplicatedStorage:FindFirstChild(script.Parent.Parent.TowerName.Value, true).Value = true

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