Hello everyone, my name is InquistorWasBanned and I am making a DataStore 2 that would save the gold that a player has. This gold should go up by 150 whenever you kill someone. I already made a DataStore2 that saves the amount but I can not seem to get it to save the gold amount if a player kills someone for gold. Is there a way to just set up a DataStore2 when I already made a different script that has my leaderstats? So would there be a way to call for example the Gold and save it instead of having to add the kill player for money script inside of the same script?
@Kampfkarren , could you help me please, this is what I got so far:
local DataStore2 = require(1936396537)
game.Players.PlayerAdded:Connect(function(plr)
print("Player Added")
local dataweap = DataStore2("Weapons",plr)
local fol = Instance.new("Folder")
fol.Parent = plr
fol.Name = "Weapons"
local tbl = {}
print("Folder Added")
if dataweap:Get() ~= nil then
for i,v in pairs(dataweap:Get())do
table.insert(tbl,v)
local tag = Instance.new("BoolValue")
tag.Parent = fol
tag.Name = v
end
else
table.insert(dataweap,"Wooden Bow")
table.insert(tbl,"Wooden Bow")
local tag = Instance.new("BoolValue")
tag.Parent = fol
tag.Name = "Wooden Bow"
table.insert(dataweap,"Wooden Sword")
table.insert(tbl,"Wooden Sword")
local tag = Instance.new("BoolValue")
tag.Parent = fol
tag.Name = "Wooden Sword"
table.insert(dataweap,"Wooden Spear")
table.insert(tbl,"Wooden Spear")
local tag = Instance.new("BoolValue")
tag.Parent = fol
tag.Name = "Wooden Spear"
dataweap:Set(tbl)
end
local function tblupdate(new)
if dataweap:Get()then
for i,v in pairs(dataweap:GetChildren())do
print(v)
if game:GetService("ReplicatedStorage").Weapons:FindFirstChild(v) then
if plr.Weapons:FindFirstChild(v)then
else
table.insert(tbl,v)
local tag = Instance.new("BoolValue")
tag.Parent = fol
tag.Name = v
end
else
table.remove(tbl,v)
table.remove(dataweap,v)
end
end
end
dataweap:Save(tbl)
end
dataweap:OnUpdate(tblupdate())
end)
I’m trying to use datastore2 to save weapons name of the player. I inserted boolvalues in the folder so I could access the player’s inventory later in the game. Could you help me fix this please, the data didn’t load up and there was no new folders in the player after I tested even though I put a boolvalue name “SaveInStudio” in the server storage.
This is my script how would I combine my kill for money script inside of my DataStore 2?
DataStore2.Combine("MasterKey","Gold")
game.Players.PlayerAdded:Connect(function(plr)
local dataGold = DataStore2("Gold", plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local gold = Instance.new("IntValue", folder)
gold.Name = "Gold"
if dataGold:Get() ~= nil then
gold.Value = dataGold:Get()
else
gold.Value = 100
end
gold.Changed:Connect(function()
dataGold:Set(gold.Value)
end)
end)
---This is the code that is using DataStore2 How would I add or plug in my Kill for Money Script on the bottom?---
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency = Instance.new("IntValue",folder)
currency.Name = "Gold"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local stats = tag.Value:WaitForChild("leaderstats")
stats["Gold"].Value = stats ["Gold"].Value + 150
end
end
end)
end)
end)
When I use both scripts it gets mixed up a little I think, because it is the same leaderstats name, Gold. Maybe you did not see my kill player for money script or would it work?
Your issue may have to do with the timing of when you are adding the “creator” tag to the humanoid of the player that has been killed. Currently, the tag is expected to be parented to the humanoid before the humanoid.Died event fires. Nothing will happen if the tag is parented to the humanoid after the humanoid.Died fires.
Alternately, just change the leaderstats “Gold” value from your kill script. Since you are listening to the changed event, the data should update when you change your leaderstats value.
That is right, but the killing script works fine, the only problem is its both the same name so its stuck in between. So I would like to replace my DataStore part where it names the leaderstats and add my kill script there, but its not working.
Why Is this Script Not Working Can Anyone help me Fix This? It is supposed to save the gold and renew every time and give gold per kill you kill a person, 150 gold
DataStore2.Combine("MasterKey","Gold")
game.Players.PlayerAdded:Connect(function(plr)
local dataGold = DataStore2("Gold", plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local gold = Instance.new("IntValue", folder)
gold.Name = "Gold"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local stats = tag.Value:WaitForChild("leaderstats")
stats["Gold"].Value = stats ["Gold"].Value + 150
if dataGold:Get() ~= nil then
gold.Value = dataGold:Get()
else
gold.Value = 100
end
gold.Changed:Connect(function()
dataGold:Set(gold.Value)
end)
end
---Why is this not working can anyone help fix this?
Your Players.PlayerAdded event is passing the parameter plr while your CharacterAdded event is attached to player:
You are initializing dataGold inside the humanoid.Died event. Move it to the PlayerAdded function. You should also be passing an initial value to the Get method rather than using the if statement:
local initialGold = 100
gold.Value = dataGold:Get(initialGold)
@Grargror I understand what you mean but how would I replace it because my whole death function requires on the character function and where would I put the functions of gold you said?
game.Players.PlayerAdded:Connect(function(plr)
local dataGold = DataStore2("Gold", plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local gold = Instance.new("IntValue", folder)
gold.Name = "Gold"
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local stats = tag.Value:WaitForChild("leaderstats")
stats["Gold"].Value = stats ["Gold"].Value + 150
if dataGold:Get() ~= nil then
gold.Value = dataGold:Get()
else
gold.Value = 100
end
gold.Changed:Connect(function()
dataGold:Set(gold.Value)
end)
end
You should be initializing stuff as indicated in the following code snippet:
game:GetService("Players").PlayerAdded:Connect(function(player)
-- This is your PlayerAdded event function.
-- Initialize your datastore and leaderstats here
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
-- This is your Humanoid.Died event function
-- Handle the died event here
end)
end)
end)
game.Players.PlayerAdded:Connect(function(plr)
local dataGold = DataStore2("Gold", plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local gold = Instance.new("IntValue", folder)
gold.Name = "Gold"
if dataGold:Get() ~= nil then
gold.Value = dataGold:Get()
else
gold.Value = 100
end
gold.Changed:Connect(function()
dataGold:Set(gold.Value)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local stats = tag.Value:WaitForChild("leaderstats")
stats["Gold"].Value = stats ["Gold"].Value + 150
end
end
local cool = false
script.Parent.Touched:Connect(function(hit)
if cool == false and hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.Parent ~= script.Parent.Parent.Parent then
cool = true
hit.Parent:FindFirstChildWhichIsA("Humanoid").Health = hit.Parent:FindFirstChildWhichIsA("Humanoid").Health - 20
-- Damage Indicator
local text = game.ReplicatedStorage.GameCrops.Dam:Clone()
text.TextLabel.Text = "-20"
text.TextLabel.TextColor3 = Color3.new(1, 0.67, 0.3)
text.TextLabel.TextSize = 40
text.Parent = hit.Parent.Head
text.Script.Disabled = false
-- Stuff for the leaderboard
if hit.Parent:FindFirstChildWhichIsA("Humanoid").Health <= 0 then
local name = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
if name then
local DataStore2 = require(1936396537)
local datakill = DataStore2("Kills", name)
local datagold = DataStore2("Golds", name)
datakill:Increment(1)
datagold:Increment(2)
end
end
wait(1)
cool = false
end
end)