So the player will purchase an item from the shop, which needs to be given to the player in such a way that it will stay in the backpack, even if they reset.
Backpack doesn't work, because it resets when the player dies.
Also, StarterGear doesn't work, because then every player will get the item.
I'm stumped...
Here's my code (temporarily using the Backpack):
01 | leaderstats = game.Players.LocalPlayer:WaitForChild( "leaderstats" ) |
02 |
03 | function onClickEvent() |
04 |
05 | print (game.Players.LocalPlayer.Name.. " is attempting a purchase." ) |
06 |
07 | if leaderstats.Tokens.Value < 0 then |
08 |
09 | print ( "Not enough tokens!" ) |
10 | script.Error:Play() |
11 |
12 | else |
13 |
14 | print ( "Ok!" ) |
15 | leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500 |
I suggest you :Clone() items from ServerStorage into the player's backpack after they die, aka when their character loads.
01 | -- server script in ServerScriptService |
02 |
03 | local boughtItems = { |
04 | game.ServerStorage.Items.Item 1 ; |
05 | game.ServerStorage.Items.Item 2 ; |
06 | } |
07 |
08 | game:GetService( "Players" ).PlayerAdded:Connect( function (p) |
09 | p.CharacterAdded:connect( function () |
10 | for a,b in next , boughtitems do |
11 | b:Clone().Parent = p:WaitForChild( "Backpack" ) |
12 | end |
13 | end ) |
14 | end ) |
You should clone the tool to the backpack, plus another one to THAT players starter gear.
01 | leaderstats = game.Players.LocalPlayer:WaitForChild( "leaderstats" ) |
02 |
03 | function onClickEvent() |
04 |
05 | print (game.Players.LocalPlayer.Name.. " is attempting a purchase." ) |
06 |
07 | if leaderstats.Tokens.Value < 0 then |
08 |
09 | print ( "Not enough tokens!" ) |
10 | script.Error:Play() |
11 |
12 | else |
13 |
14 | print ( "Ok!" ) |
15 | leaderstats.Tokens.Value = leaderstats.Tokens.Value - 1500 |
and btw, you should do these things on client side. Only the client would see the leaderstats change. You should use remote events to make it seen to the server.
01 | local savedBackpack = { } -- Creating a table for our backpack, we'll iterate through this later. |
02 | local Player = game:GetService( "Players" ).LocalPlayer |
03 |
04 | -- When the user purchases an item, |
05 |
06 | local purchasedItem = game.ReplicatedStorage.Items.ClassicLauncher:Clone() |
07 | purchasedItem.Parent = game.Players.LocalPlayer:WaitForChild( "Backpack" ) |
08 | table.insert(savedBackpack, purchasedItem); |
09 |
10 | -- When the player dies, |
11 |
12 | for _,v in pairs (savedBackpack) do |
13 | v.Parent = Player:WaitForChild( "BackPack" ) |
14 | end |
Please for the love of god use tables for this, don't parent anything to a storage like others have sugguested.
01 | --put in ServerScriptService |
02 | local Storage = Instance.new( "Folder" ) |
03 | Storage.Parent = game:GetService( "ServerStorage" ) |
04 | Storage.Name = "Storage" |
05 | game:GetService( "Players" ).PlayerAdded:connect( function (plr) |
06 | if not Storage:FindFirstChild(plr.Name) then --Creates player storage |
07 | local Folder = Instance.new( "Folder" ) |
08 | Folder.Parent = Storage |
09 | Folder.Name = plr.Name |
10 | end |
11 | plr.CharacterAdded:connect( function (char) |
12 | for i,v in pairs (Storage [ plr.Name ] :GetChildren()) do --Loads player data |
13 | v.Parent = char |
14 | end |
15 | char.Humanoid.Died:connect( function () |