Cannot increase Hunger when Clicking Part

Hello! I can’t wrap my head around this but I tried to make a hunger system. Everything works well but I cannot make a Part bring food when clicked…

Problem code:

local debounce = false
local ClickDetect = script.Parent.ClickDetector
local player = game.Players.LocalPlayer

script.Parent.ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true	
		local Hunger = player.leaderstats.Hunger
		player.leaderstats.Hunger.Value += 1 		
		script.Parent.Transparency = 1 -- Makes Block Invisible	
		ClickDetect.MaxActivationDistance = 0	
		wait(600) -- Changes how long the block is not visible	
		script.Parent.Transparency = 0 -- Makes block visible again
		ClickDetect.MaxActivationDistance = 10			
		debounce = false
	end
end)

leaderstats code:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Value = 100 
	Hunger.Parent = leaderstats
	
	local Thirst = Instance.new("IntValue")
	Thirst.Name = "Thirst"
	Thirst.Value = 100 
	Thirst.Parent = leaderstats
	
end
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

Problem is: I cannot increase the Hunger IntValue… Can anybody help?

1 Like

Maybe using a RemoteEvent would work? If you add a remote event to ReplicatedStorage and call it something like “HungerIncrease” and use it to increase the player’s leaderstats from a server script, it might work.

This would be your local script

local remoteEvent = game.ReplicatedStorage.HungerIncrease

local debounce = false
local ClickDetect = script.Parent.ClickDetector
local player = game.Players.LocalPlayer

script.Parent.ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true
		remoteEvent:FireServer()	
		script.Parent.Transparency = 1 -- Makes Block Invisible	
		ClickDetect.MaxActivationDistance = 0	
		wait(600) -- Changes how long the block is not visible	
		script.Parent.Transparency = 0 -- Makes block visible again
		ClickDetect.MaxActivationDistance = 10			
		debounce = false
	end
end)

And then insert a normal script into ServerScriptService

local remoteEvent = game.ReplicatedStorage.HungerIncrease

remoteEvent.OnServerEvent:Connect(function(player)
    player.leaderstats.Hunger.Value += 1
end)

You can learn more about RemoteEvents on the DevHub or YouTube.

Hello! The Part doesn’t seem to dissapear (as in my script, putting the transparency to 1) neither give me +1 Hunger
Does my Script have to be a normal or Local script?

If your script is a server script, then you can’t find the local player in it. So that’s why it can’t increase the value because the “player” doesn’t exist.
You could solve this by making a player argument when the player clicks the click detector:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
Then the script can find the player when the ClickDetector is clicked.

However if it’s not a serverscript (but a localscript) please tell so.

1 Like

It is definetly a Server-Sided Script, as I experimented and ClickDetectors work best on Server Scripts. I will try that.

2 Likes

YES! So instead of using a RemoteEvent I used the (function(player) and put my player.leaderstats.Hunger.Value += 1

Thank you both for your answers

2 Likes