What do you want to achieve? I want a tool to be added in the player’s backpack after checking a value.
What is the issue? The tool does not appear in the backpack.
Output:
game.Players.PlayerAdded:Connect(function(Player)
local OwnedItems = Player:WaitForChild("OwnedItems")
local M60 = OwnedItems:WaitForChild("M60")
if M60.Value == 1 then
local Tool = game.ReplicatedStorage.M60
local ToolClone = Tool:Clone()
local Backpack = Player:WaitForChild("Backpack")
ToolClone.Parent = Backpack
print(ToolClone.Parent)
print("Finished")
end
end)
game.Players.PlayerAdded:Connect(function(Player)
local OwnedItems = Player:WaitForChild("OwnedItems")
local M60 = OwnedItems:WaitForChild("M60")
if M60.Value == 1 then
local Tool = game.ReplicatedStorage.M60
local ToolClone = Tool:Clone()
local Backpack = Player:WaitForChild("Backpack")
ToolClone.Parent = Backpack
print(ToolClone.Parent)
print("Finished")
else print("You don't have the M60")
end
end)
Maybe I’m not understanding, but what you’re saying only aligns with what I originally said.
Your script only adds the tool when the value is equal to 1, but it outputs 0. This means that the value is 0 at the exact moment that your script checks the value.
That’s alright. By the looks of it, you seem to be using NumberValues most likely. Values generally have a default state of nil, 0, or a blank string, depending on the type of value. In the case of NumberValues, the default value will be 0.
It really depends on how you’re going about things, but in this case, I would recommend using a Changed event to check when the value changes. If it’s ever set to 1, clone the tool into the backpack. If it’s ever set to 0, you can optionally remove it as well.
Yes, but I’m trying to get the system to work when the player spawns/respawns. (If you buy a gun, the value of the IntValue is set to 1 and you get a gun in your backpack, but when you die of course you don’t have it anymore. So, I’m trying to make a script that gives you the gun when when you respawn)
As usual, there are multiple ways to go about this. But I think the easiest solution is to add a third number. Where 0 means it hasn’t loaded, and then have 1 and 2 determine whether or not the player owns said item. In this scenario, you can yield until the value does not equal 0.
Another simple solution exists if you’re not working with DataStorage. Instead, you can insert tools the player “owns” into StarterGear. They will automatically spawn with those items in their backpack. Note that this only happens if the tools are in the StarterGear before spawning. So if you insert it after, you will also have to insert it into the player’s Backpack.
I will also say that there are better methods for inventory management, such as tables, but take your time.
Have you tried parenting the tool every time their character loads?
--Script inside StarterCharacterScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local OwnedItems = Player:WaitForChild("OwnedItems")
local M60 = OwnedItems:WaitForChild("M60")
if M60.Value == 1 then
local Tool = ReplicatedStorage.M60
local ToolClone = Tool:Clone()
local Backpack = Player:WaitForChild("Backpack")
ToolClone.Parent = Backpack
print(ToolClone.Parent)
print("Finished")
end