Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Increase Hunger when clicking brick/part/food?

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?

2 answers

✓ Accepted answer

First Of All. If This Is In A Local Script I Recommend To Change It To Script.

You Can Interact With Parts In The Workspace With LocalScript But It Only Happens Client Side.

When You Have Changed The LocalScript To A Script I Re Did The Code A Little So You Can Copy It

Heres are Some Things I Noticed Here.

First Of All, You Can Do This Sever Sided

ClickDetector Has A Built-In Function For Getting The Player So LocalScripts Are Useless When Interacting With Clickdetectors. Cause The Events Will Only Happen Client Side.

The Method you are Using To Add Up The Value Of Leaderstats Does Work. But It Can Get Thrown Off Easily so instead, I recommend to use next time

THIS IS JUST AN EXAMPLE

player.leaderstats.Value.Value = player.leaderstats.Value.Value + your amount

One Extra Thing Is. With Clickdetectors Insteand Of Using MaxActivationDistance To Enable In Disable It You Can Shorten In Up By Doing Clickdetector.Enabled = your boolean here

local debounce = false
local ClickDetect = script.Parent.ClickDetector

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

4 comments

0

It is already a Server-Side Script.

0

I Fixed Up The Code For The Server Side Script.

0

WAit I didn't look at the (function(player) xd yep this is the answer! I made a more clean version, too, where you don't need the (Local Hunger)

0

Also You Can Get The Player From A Server Side Script. Cause It Doesnt No Who To Direct It To

So, because this is a server sided script, I cannot get the local player using local, but I can get it using :Connect(function(player) Resulting in:

local debounce = false
local ClickDetect = script.Parent.ClickDetector

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

This discussion is preserved as it was. New answers, comments, and votes are closed.