So I’ve never done a datastore for values not in leaderstats. I tried to do one so that the IntValue holding the wood resources was saved and when a player joined it would update the Gui. When I join the game the value in the GUI does not change. Very suspiciously, it works in studio (After I go in game and mine the log).
Datastore:
local resourcesDataStore = game:GetService("DataStoreService"):GetDataStore("resourcesDataStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local resourcesStored = Instance.new("Folder")
resourcesStored.Name = "resourcesStored"
resourcesStored.Parent = plr
local woodResources = Instance.new("IntValue")
woodResources.Name = "woodResources"
woodResources.Parent = resourcesStored
woodResources.Value = 0
plr.PlayerGui:WaitForChild("ScreenGui")
plr.PlayerGui.ScreenGui.Frame.WoodValue.Text = woodResources.Value
local playerKey = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = resourcesDataStore:GetAsync(playerKey)
end)
if success then
if data then
plr:WaitForChild("resourcesStored").woodResources.Value = data
else
plr:WaitForChild("resourcesStored").woodResources.Value = 0
end
else
print(errormessage)
end
end)
Players.PlayerRemoving:Connect(function(plr)
local playerKey = "Player_"..plr.UserId
local success, errormessage = pcall(function()
resourcesDataStore:SetAsync(playerKey, plr.resourcesStored.woodResources.Value)
end)
if not success then
print(errormessage)
end
end)
PlayerAdded Script that Updates Gui:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
plr.PlayerGui:WaitForChild("ScreenGui")
plr.PlayerGui.ScreenGui.Frame.WoodValue.Text = plr.resourcesStored.woodResources.Value
end)
Tool that sets data when wood is chopped:
local event = script.Parent:WaitForChild('RemoteEvent')
local canhit = false
local toolhandle = script.Parent:FindFirstChild('Handle')
local hitwoodsound = script.Parent:WaitForChild('Handle').WoodSound
local toolswingsound = script.Parent:WaitForChild('Handle').ToolSwing
event.OnServerEvent:Connect(function(plr)
toolswingsound:Play()
canhit = true
toolhandle.Touched:Connect(function(hit)
if hit.Parent.Name ~= plr.Name then
if hit.Name == 'Log' and canhit then
print(plr.Name..' hit a log.')
hitwoodsound:Play()
canhit = false
hit.Durability.Value -= 1
if hit.Durability.Value <= 0 then
wait(0.5)
hit:Destroy()
plr.PlayerGui:WaitForChild("ScreenGui")
plr.PlayerGui.ScreenGui.Frame.WoodValue.Text += 1
plr.resourcesStored.woodResources.Value += 1
end
end
end
end)
wait(1)
canhit = false
end)