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

Gui StringValue not working with ReplicatedStorage?

I am trying to have a StringValue as the name of the item in ReplicatedStorage. For example, the ItemName value is "Sword" but it says in output that ("itemName is not a valid member of Folder") How can I make this work?

script.Parent.MouseButton1Click:connect(function()
	local RS = game:GetService('ReplicatedStorage')
	local itemName = script.Parent.ItemName.Value
	local item = RS.Weps.itemName
	local itemPrice = script.Parent.PriceValue.Value
	--local itemPrice = 100
	local Player = game.Players.LocalPlayer
	local stats = Player:WaitForChild('leaderstats')
	
	if stats.Money.Value >= itemPrice then
		print("Item Purchased!")
		stats.Money.Value = stats.Money.Value - itemPrice
		print("Player has recieved the item!")		
		local itemCloned = item:Clone()
		itemCloned.Parent = Player.Backpack
		itemCloned.Parent = Player.StarterPack	
	end
end)

1 comment

0

Make sure to use WaitForChild, as things can sometimes take time to load: local item = RS.Weps:WaitForChild("itemName")

1 answer

✓ Accepted answer

WaitForChild works in this situation. Sometimes, it takes time for things to load. The script is run before the item is loaded so it is treated like nothing.

Here is the edited script:

script.Parent.MouseButton1Click:connect(function()
    local RS = game:GetService('ReplicatedStorage')
    local itemName = script.Parent:WaitForChild("Itemname").Value
    local item = RS.Weps:WaitForChild("itemName") -- Waits for this object to load.
    local itemPrice = script.Parent.:WaitForChild("PriceValue").Value
    --local itemPrice = 100
    local Player = game.Players.LocalPlayer
    local stats = Player:WaitForChild("leaderstats")

    if stats.Money.Value >= itemPrice then
        print("Item Purchased!")
        stats.Money.Value = stats.Money.Value - itemPrice
        print("Player has recieved the item!")      
        local itemCloned = item:Clone()
        itemCloned.Parent = Player:WaitForChild("Backpack")
        itemCloned.Parent = Player:WaitForChild("StarterPack")
    end
end)

If you have any problems, please leave a comment below. Thank you and I hope this will help you.

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