Material +1 script not working

Hello, i have this issue where when a player collects a log the value of wood wont + 1 and stays 0.

does anybody know why?
here is my code:


local dataservice = game:GetService("DataStoreService")
local leaderstats = dataservice.leaderstats
local wood = leaderstats.Wood

script.Parent.Triggered:Connect(function(plr)

	script.Parent.Parent.Parent:Destroy()
	wood += 1


end)
2 Likes

do please note that the variables are just me playing around, ignore them for now.

local dataservice = game:GetService("DataStoreService")
local leaderstats = dataservice.leaderstats
local wood = leaderstats.Wood

script.Parent.Triggered:Connect(function(plr)
	script.Parent.Parent.Parent:Destroy()
	wood.Value += 1
end)
2 Likes

Samething i did but no spaces. not work. ty for the help tho

Try this:

script.Parent.Triggered:Connect(function(plr)
    local wood = plr:WaitForChild("leaderstats"):WaitForChild("Wood")
	wood = wood + 1
	script.Parent.Parent.Parent:Destroy()
end)

Are you trying to change leaderstats.Wood IN A PLAYER?

script.Parent.Triggered:Connect(function(plr)
    plr.leaderstats.Wood.Value += 1
    script.Parent.Parent.Parent:Destroy()
end)

if you wanna SAVE IT in DSS than you should try this instead

local Datastore = game:GetService("DataStoreService"):GetDataStore("Wood")
script.Parent.Triggered:Connect(function(plr)
    local Wood = DataStore:GetAsync(plr.Name)
    if not Wood then
        DataStore:SetAsync(plr.Name,0)
    end
    plr.leaderstats.Wood.Value += 1
    DataStore:SetAsync(plr.Name,Wood+1)
    script.Parent.Parent.Parent:Destroy()
end)

This will save it.
To load it when the player joins you should INSERT THIS in your leaderstats script

--...Your script
-- PLAYER = your player variable at .PlayerAdded- Change it to that
-- WOODVALUE = your leaderstats.Wood value variable- Change it to that
local DSS = game:GetService("DataStoreService")
local WoodStore = DSS:GetDataStore("Wood")
if not WoodStore:GetAsync(PLAYER.Name) then
    WoodStore:SetAsync(PLAYER.Name,0)
end
WOODVALUE.Value = WoodStore:GetAsync(PLAYER.Name)
--...Your script

This code will error because you’re trying to add 1 to an object, not a value.

script.Parent.Triggered:Connect(function(plr)
    local wood = plr:WaitForChild("leaderstats"):WaitForChild("Wood")
	wood.Value += 1
end)

This should do the trick.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.