Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Gui StringValue not working with ReplicatedStorage?

Asked by 8 years ago

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?

01script.Parent.MouseButton1Click: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
18end)
0
Make sure to use WaitForChild, as things can sometimes take time to load: local item = RS.Weps:WaitForChild("itemName") TheDeadlyPanther 2460 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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:

01script.Parent.MouseButton1Click: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
18end)

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

Ad

Answer this question