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?
01 | script.Parent.MouseButton 1 Click:connect( function () |
02 | local RS = game:GetService( 'ReplicatedStorage' ) |
03 | local itemName = script.Parent.ItemName.Value |
04 | local item = RS.Weps.itemName |
05 | local itemPrice = script.Parent.PriceValue.Value |
06 | --local itemPrice = 100 |
07 | local Player = game.Players.LocalPlayer |
08 | local stats = Player:WaitForChild( 'leaderstats' ) |
09 |
10 | if stats.Money.Value > = itemPrice then |
11 | print ( "Item Purchased!" ) |
12 | stats.Money.Value = stats.Money.Value - itemPrice |
13 | print ( "Player has recieved the item!" ) |
14 | local itemCloned = item:Clone() |
15 | itemCloned.Parent = Player.Backpack |
16 | itemCloned.Parent = Player.StarterPack |
17 | end |
18 | 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:
01 | script.Parent.MouseButton 1 Click:connect( function () |
02 | local RS = game:GetService( 'ReplicatedStorage' ) |
03 | local itemName = script.Parent:WaitForChild( "Itemname" ).Value |
04 | local item = RS.Weps:WaitForChild( "itemName" ) -- Waits for this object to load. |
05 | local itemPrice = script.Parent.:WaitForChild( "PriceValue" ).Value |
06 | --local itemPrice = 100 |
07 | local Player = game.Players.LocalPlayer |
08 | local stats = Player:WaitForChild( "leaderstats" ) |
09 |
10 | if stats.Money.Value > = itemPrice then |
11 | print ( "Item Purchased!" ) |
12 | stats.Money.Value = stats.Money.Value - itemPrice |
13 | print ( "Player has recieved the item!" ) |
14 | local itemCloned = item:Clone() |
15 | itemCloned.Parent = Player:WaitForChild( "Backpack" ) |
16 | itemCloned.Parent = Player:WaitForChild( "StarterPack" ) |
17 | end |
18 | end ) |
If you have any problems, please leave a comment below. Thank you and I hope this will help you.