Goal/Aim
- I am trying to write a script that chops trees and awards the player a random amount of [treeType]. This all works fine and what not. However, I’m having trouble getting the DataStore to save the values this script outputs.
Script
-- LocalScript inside Axe (tool)
local tool = script.Parent
local player = game.Players.LocalPlayer
local oreInventory = player.OreInventory
local mouse = player:GetMouse()
local equipped = false
local handle = tool.Handle
local can_cut = false
local debounce = false
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
mouse.Button1Down:connect(function()
if equipped and debounce == false then
debounce = true
can_cut = true
end
end)
handle.Touched:connect(function(hit)
if equipped and can_cut then
can_cut = false
if (string.lower(hit.Parent.Name) == "tree") then
local tree = hit.Parent
local treeHealth = tree:WaitForChild("HP")
local treeType = tree:WaitForChild("TreeType").Value
if treeHealth.Value > 0 then
treeHealth.Value = treeHealth.Value - 1
oreInventory[treeType].Value = oreInventory[treeType].Value + math.random(1,2)
print(treeHealth.Value)
else
for i,v in pairs(tree:GetChildren()) do
if v:IsA("Part") then
for i = 1,10 do
v.Transparency = (i/10)
game:GetService("RunService").Heartbeat:Wait()
end
v:Destroy()
end
end
end
end
wait(1)
debounce = false
end
end)
And secondly,
-- Script in ServerScriptService
local dataService = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")
local petStore = dataService:GetDataStore("Pets")
local moneyStore = dataService:GetDataStore("Money")
local oreStore = dataService:GetDataStore("Ore")
game.Players.PlayerRemoving:connect(function(player)
local playerKey = tostring(player.UserId)
local stats = player.leaderstats
local pets = {}
local money = stats.Money
local ore = {}
local ingots = {}
local petInventory = player:WaitForChild("PetInventory")
local oreInventory = player:WaitForChild("OreInventory")
for i,v in pairs(petInventory:GetChildren()) do
table.insert(pets, v.ID.Value)
end
for i,v in pairs(oreInventory:GetChildren()) do
table.insert(ore, (v.Name.."-"..v.Value))
end
local petJSON = httpService:JSONEncode(pets)
local moneyJSON = httpService:JSONEncode(money)
local oreJSON = httpService:JSONEncode(ore)
petStore:SetAsync(playerKey, petJSON)
moneyStore:SetAsync(playerKey, moneyJSON)
oreStore:SetAsync(playerKey, oreJSON)
end)
game.Players.PlayerAdded:connect(function(player)
local playerKey = tostring(player.UserId)
local stats = player:WaitForChild("leaderstats")
local petInventory = player:WaitForChild("PetInventory")
local oreInventory = player:WaitForChild("OreInventory")
local petStorage = game.ReplicatedStorage.Pets
local pets = {}
local ore = {}
local money = {}
local ingots = {}
local petStoreLOAD = petStore:GetAsync(playerKey)
local petJSON = httpService:JSONDecode(petStoreLOAD)
local moneyLOAD = moneyStore:GetAsync(playerKey)
local moneyJSON = httpService:JSONDecode(moneyLOAD)
local oreLOAD = oreStore:GetAsync(playerKey)
local oreJSON = httpService:JSONDecode(oreLOAD)
for _,pet in pairs(petJSON) do
for _,target in pairs(petStorage:GetChildren()) do
if target.ID.Value == pet then
local newPet = target:Clone()
newPet.Parent = petInventory
end
end
end
for _,ore in pairs(oreJSON) do
local hyphen = string.find(ore, "-")
local oretype = string.sub(ore, 1, (hyphen-1))
local quantity = tonumber(string.sub(ore, (hyphen+1), string.len(ore)))
for _,target in pairs(oreInventory:GetChildren()) do
if target.Name == oretype then
target.Value = quantity
end
end
end
end)
I have tried looking at other posts and websites for help to at least try and understand a source of errors yet I seem to fail to find one. Any help would be greatly appreciated!