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)
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.