What do you want to achieve? Keep it simple and clear!
I am trying to add a tool named ‘tool’ to a players backpack if a leaderstat is at value of 1
What is the issue? Include screenshots / videos if possible!
The script i have made doesn’t give any errors but also does not give the tool to the player
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have looked at how other people are adding tools to a backpack but they arent requiring a leaderstat value before doing so
The script i have at the moment is below.
-- Add Tool to players backpack
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
if leaderstats.Pistol.Value == 1
then
local tool = game.ReplicatedStorage.Tools.Tool:Clone()
print(tool.Name)
tool.Parent = player.Backpack
end
end)
What you actually should do is use the leaderstats creation in the same script. This is because, when the leaderstats are added, they don’t have anything inside. Continuing, when “Pistol” is added, the value is 0/nil. The datastores take a few seconds to load so therefor you need to either put both in the same script or fire a bindableEvent when the value is added.
The script is a plugin made by snoopy1333 which has values that are saved in the serverscriptsservice which are then used to create the leaderstats values
local DataStore = game:GetService(“DataStoreService”):GetDataStore(script.DatstoreId.Value)
local Folder
local function FindFolder()
for i,v in pairs(script:GetChildren()) do
if v.ClassName == “Folder” then
Folder = v
end
end
end
local Clone
local function LoadInstances(plr)
FindFolder()
Clone = Folder:Clone()
Clone.Parent = plr
end
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
if Clone then
local SaveData = {}
for i,v in pairs(Clone:GetChildren()) do
SaveData[v.Name] = v.Value
end
DataStore:SetAsync(player.UserId, SaveData)
end
end)
end)
local Data = nil
pcall(function()
Data = DataStore:GetAsync(plr.UserId)
if Data ~= nil then
for i,v in pairs(Clone:GetChildren()) do
v.Value = Data[v.Name]
end
end
end)
You can totally do that manually, just a simple data store. And you have more control over it.
Like I was thinking that uses a DataStore, which takes around 1 second to load. But you don’t know this, so you can’t just do wait(1) as sometimes it’s more than that. So, unfortunately you either have to rework some of the plugin to fire a bindableEvent or add the tool. Or, what I reccomend doing, making your own dataStore. It’s very simple and you gather some knowledge. And it allows for more control of what it’s doing
would i do the same as when creating a leaderboard? i have managed to get a leaderboard working lol.
Basically i am making a military game where i want the player to be able to select weapons and then teleport but keep those weapons. I read about teleport data but also saw that it isnt recommended for player inventory so i thought using a simple 0 = player doesnt have weapon and 1 = player has weapon might work on a datastore. Is this an acceptable way to create such a thing?
-- This part of the script will give a tool to a player when they respawn. In your old script, players had to rejoin to receive the tool.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Leaderstats = Player:WaitForChild("leaderstats")
if Leaderstats then
if Leaderstats.Pistol.Value == 1 then
[Your tool]:CLone().Parent = Player.Backpack
end
else
warn("Leaderstats not found!")
end
end)
end)
-- This part of the script will give a tool to a player when the value of "Pistol" change.
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Player:WaitForChild("leaderstats")
if Leaderstats then
Leaderstats.Pistol.Changed:Connect(function()
if Leaderstats.Pistol.Value == 1 then
[Your tool]:CLone().Parent = Player.Backpack
end
end)
else
warn("Leaderstats not found!")
end
end)