Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Increase Hunger when clicking brick/part/food?

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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)
0
It is already a Server-Side Script. qMrSpooky 20 — 3y
0
I Fixed Up The Code For The Server Side Script. CallMe_Axis 63 — 3y
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) qMrSpooky 20 — 3y
0
Also You Can Get The Player From A Server Side Script. Cause It Doesnt No Who To Direct It To CallMe_Axis 63 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)

Answer this question