Script with proximity prompt not work while code should be right

hello i have this issue with my code where when a player destroys a tree a proximity prompt shows up to collect the wood but when the player holds E and finishes the prompt then nothing happens.
here is my code: (do note that im new to coding)

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

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

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


end)

here is my leaderstats script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Wood = Instance.new("IntValue")
	Wood.Name = "Wood"
	Wood.Parent = leaderstats
	
	local Stone = Instance.new("IntValue")
	Stone.Name = "Stone"	
	Stone.Parent = leaderstats	
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
end)

Here is a picture of where my stuff is parented to.

leaderstats:
image

script for proximity prompt:

image

please help me if you know the answer.

Switch these around.
Also, set a value for you int values, even if it is 0.

what do you mean im new to coding.

im pretty sure he means that basically the script is being destroyed before the value can change, actually im pretty sure that is the reason why the value isnt changing


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

And Wood.Value = 0 etc when you create the new int values for leaderstats

i changed to

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

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




end)

and leaderstat:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Wood = Instance.new("IntValue")
	Wood.Name = "Wood"
	Wood.Parent = leaderstats
	
	local Stone = Instance.new("IntValue")
	Stone.Name = "Stone"	
	Stone.Parent = leaderstats	
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
	
	Wood.Value = 0
	Stone.Value = 0
	Level.Value = 0
end)

still wont work

what is the reason of if wood.Value == wood.Value + 1 then

i wait if it changes to that and then do the next

i removed it but it wont work still

show a video of your game so i can see whats happening

robloxapp-20230415-1510060.wmv (1.5 MB)

add prints inbetween every line of code that is in the function

Lol, missed it first time round! The leaderstats folder is parented to the player, but the function connects to a leaderstats in the DatastoreService.
You will need to use player service and find the player.

Wood = game:GetService("Players"):FindFirstChild(plr).Leaderstats.Wood

Add this inside the first function.
(Typing on phone may have misspelt something)

oh shoot i missed that too lol

and i did all prints but no printed. i changed the code to this:

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


script.Parent.Triggered:Connect(function(plr)
	wood = game:GetService("Players"):FindFirstChild(plr).Leaderstats.Wood
	wood.Value = wood.Value + 1
	print('succes + 1')
	script.Parent.Parent:Destroy()
	print('succes destroyed')
		
	end




end)

and one of the ends has a red line under it.

local dataservice = game:GetService(“DataStoreService”)
local leaderstats = dataservice.leaderstats
local wood = leaderstats.Wood
remove that
and theres a extra end hanging there which causes the error that happened because you didnt remove the end for if wood.Value == wood.Value + 1 then

script.Parent.Triggered:Connect(function(plr)
	local wood = game:GetService("Players"):FindFirstChild(plr).Leaderstats.Wood
	wood.Value = wood.Value + 1
	print('succes + 1')
	script.Parent.Parent:Destroy()
	print('succes destroyed')
end)

i still have a error or red circle at the end after removing them

Workspace.Tree4.TreeStem2.ProximityPrompt.Script:2: attempt to index nil with ‘Leaderstats’
got this error

Check capitalisation. On phone so aut capitalises stuff sometimes.

because Leaderstats name isnt Leaderstats but it is leaderstats lowercased

1 Like