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

How to copy a tool from ReplicatedStorage to a players backpack?

Asked by 10 years ago

This is my script so when you click a Gui Button and you have enough money, it clones a tool in Replicated storage and puts it in the player who bought it's backpack/inventory

player=game.Players.LocalPlayer --This creates a variable for the player
Points=player:WaitForChild("leaderstats"):WaitForChild("Points") --This makes a variable for their "Cash" value in leaderstats. If there isn't one, then this would cause an error. Giving us the output improves the likelyhood of us being able to give a good answer. I would replace this line with Points=player:WaitForChild("leaderstats"):WaitForChild("Cash")
upgradeStuff = game.ReplicatedStorage.GravityCoil --This makes a vairable for the item being bought. Using ReplicatedStorage for storing things is better than Lighting tho.

function buy()
if Points.Value >= 25 then --Cost

    Points.Value = Points.Value - 25 --Cost

        upgradeStuff:Clone().Parent = player.Backpack --This gives them the gravity coil


else --If they don't have enough points
        script.Parent.Parent.Text = "You cant afford this!" 
    wait(3)--Wait a bit
        script.Parent.Parent.Text = "Gravity Coil"  
end
end

script.Parent.MouseButton1Down:connect(buy)

The problem is it doesnt put the Gravity coil in the inventory

2 answers

Log in to vote
0
Answered by
Necrorave 560 Moderation Voter
10 years ago

Try assigning the clone of that object to a variable. Then setting the parent to player.Backpack

player=game.Players.LocalPlayer --This creates a variable for the player
Points=player:WaitForChild("leaderstats"):WaitForChild("Points") --This makes a variable for their "Cash" value in leaderstats. If there isn't one, then this would cause an error. Giving us the output improves the likelyhood of us being able to give a good answer. I would replace this line with Points=player:WaitForChild("leaderstats"):WaitForChild("Cash")
upgradeStuff = game.ReplicatedStorage.GravityCoil --This makes a vairable for the item being bought. Using ReplicatedStorage for storing things is better than Lighting tho.

function buy()
if Points.Value >= 25 then --Cost

    Points.Value = Points.Value - 25 --Cost

    clone = upgradeStuff:Clone() -- Setting the clone to the variable "clone"
    clone.Parent = player.Backpack -- setting "clone"'s parent to player.Backpack


else --If they don't have enough points
        script.Parent.Parent.Text = "You cant afford this!" 
    wait(3)--Wait a bit
        script.Parent.Parent.Text = "Gravity Coil"  
end
end

script.Parent.MouseButton1Down:connect(buy)


Give this a try, let me know if there are any issues.

Ad
Log in to vote
0
Answered by 10 years ago

I would try doing a :FindFirstChild(). That would ensure it finds the gravity coil.

player=game.Players.LocalPlayer --This creates a variable for the player
Points=player:WaitForChild("leaderstats"):WaitForChild("Points") --This makes a variable for their "Cash" value in leaderstats. If there isn't one, then this would cause an error. Giving us the output improves the likelyhood of us being able to give a good answer. I would replace this line with Points=player:WaitForChild("leaderstats"):WaitForChild("Cash")
upgradeStuff = game.ReplicatedStorage:FindFirstChild("GravityCoil") --This makes a vairable for the item being bought. Using ReplicatedStorage for storing things is better than Lighting tho.

function buy()
    if Points.Value >= 25 then --Cost
        Points.Value = Points.Value - 25 --Cost
        local clone = upgradeStuff:Clone() -- Setting the clone to the variable "clone"
        clone.Parent = player.Backpack -- setting "clone"'s parent to player.Backpack
    else --If they don't have enough points
        script.Parent.Parent.Text = "You cant afford this!"
        wait(3)--Wait a bit
        script.Parent.Parent.Text = "Gravity Coil" 
    end
end

script.Parent.MouseButton1Down:connect(buy)

Answer this question