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

Why does it give me an "attempt to index nil with 'FindFirstChild'"?

someone for the love of god help me I've had 150 million people look at this and none of them can figure out what's going on

https://imgur.com/a/YFpPvL7

Errors are on lines 16 on HungerDmg and ThirstDmg

Stats

local config = script.Configuration -- locate config folder
local drain = script.Drain -- locate drain script
local checkHunger = script.HungerDmg -- locate hunger damage script
local checkThirst = script.ThirstDmg -- locate thirst damage script
local playerID = script.FuckRoblox

game.Players.PlayerAdded:Connect(function(plr) -- when player joins
	playerID.Value = plr.UserId
	local leaderstats = Instance.new("Folder") -- create folder
	leaderstats.Name = "PlayerStats" -- name folder "PlayerStats"
	leaderstats.Parent = plr -- Set playerstat folder parent to the player
	
	local MaxHunger = Instance.new("NumberValue") --create max hunger value
	MaxHunger.Name = "MaxHunger" -- name it
	MaxHunger.Parent = leaderstats -- parent it to PlayerStats
	MaxHunger.Value = 100 -- set value to 100
	
	local hunger = Instance.new("NumberValue")
	hunger.Name = "Hunger" -- name it
	hunger.Parent = leaderstats -- parent it to PlayerStats
	hunger.Value = 5-- set value to max value
	
	local MaxThirst = Instance.new("NumberValue")
	MaxThirst.Name = "MaxThirst" -- name it
	MaxThirst.Parent = leaderstats -- parent it to PlayerStats
	MaxThirst.Value = 100 -- set value to 100
	
	local thirst = Instance.new("NumberValue")
	thirst.Name = "Thirst" -- name it
	thirst.Parent = leaderstats -- parent it to PlayerStats
	thirst.Value = MaxThirst.Value -- set value to max value
	
end)

drain.Disabled = false -- start drain
checkHunger.Disabled = false
checkThirst.Disabled = false

Drain - disabled by defoolt

local config = script.Parent.Configuration -- config folder
local tbr = config.TimeBetweenReduction-- time between thirst reduction

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("PlayerStats") -- define player stats folder
	thirstStat = leaderstats:WaitForChild("Thirst") -- define thirst stat
	hungerStat = leaderstats:WaitForChild("Hunger") -- yare yare daze
end)

while true do
	wait(tbr.Value)
	if thirstStat.Value > 0 then -- if stat is more than 0 then drain
		thirstStat.Value = thirstStat.Value - 1 -- drains a certain amount of the stat
	end
	if hungerStat.Value > 0 then -- if stat is more than 0 then drain
		hungerStat.Value = hungerStat.Value - 1 -- drains a certain amount of the stat
	end
end

HungerDmg - disabled by defoolt

local config = script.Parent.Configuration -- locate config folder where dem shiz are
local thirstDamage = config.ThirstDamage -- how much damage thirst does
local hungerDamage = config.HungerDamage -- how much damage hunger does

local playerID = script.parent.FuckRoblox



game.Players.PlayerAdded:Connect(function(player)
	script.Parent.FuckRoblox.Value = player.UserId
	print("Player ID is: "..script.Parent.FuckRoblox.Value)
end)

local plr = game:GetService("Players"):GetPlayerByUserId(script.Parent.FuckRoblox.Value)

local PlrStats = plr:FindFirstChild("PlayerStats") -- locate player stats folder
local hungerStat = PlrStats:WaitforChild("Hunger") -- locate hunger stat within player stats

local h = plr.Character.Humanoid -- assign humanoid

while true do
	wait(3) -- Time between damage
	if hungerStat.Value <= 0 and h.Health > 0 then -- if thirst is less or equal to 0 and player hp is greater than 0
		h:TakeDamage(hungerDamage.Value) --Player takes the defined amount of damage 
	end

end

ThirstDmg - disabled by defotueotl

local config = script.Parent.Configuration -- locate config folder where dem shiz are
local thirstDamage = config.ThirstDamage -- how much damage thirst does
local hungerDamage = config.HungerDamage -- how much damage hunger does

local playerID = script.parent.FuckRoblox



game.Players.PlayerAdded:Connect(function(player)
	script.Parent.FuckRoblox.Value = player.UserId
	print("Player ID is: "..script.Parent.FuckRoblox.Value)
end)

local plr = game:GetService("Players"):GetPlayerByUserId(script.Parent.FuckRoblox.Value)

local PlrStats = plr:FindFirstChild("PlayerStats") -- locate player stats folder
local thirstStat = PlrStats:WaitforChild("Thirst") -- locate hunger stat within player stats

local h = plr.Character.Humanoid -- assign humanoid

while true do
	wait(3) -- Time between damage
	if thirstStat.Value <= 0 and h.Health > 0 then -- if thirst is less or equal to 0 and player hp is greater than 0
		h:TakeDamage(hungerDamage.Value) --Player takes the defined amount of damage 
	end

end

3 comments

0

i dont even know what to say anymore ive made such a messy shit show of doe ill probably faint soon

0

There's no player stats in a playerID

0

but htere is?

2 answers

-- Script in ServerScriptService

local config = script:FindFirstChild"Configuration" -- locate config folder where dem shiz are
local thirstDamage = config and config.ThirstDamage.Value or 10 -- how much damage thirst does
local hungerDamage = config and config.HungerDamage.Value or 20 -- how much damage hunger does
local max_thirst,max_hunger=100,100
local starting_thirst,starting_hunger=max_thirst,max_hunger




local Players=game:GetService"Players"
local function drain(args)
local enabled,char,thirst,hunger,hum=unpack(args)
    while enabled do
        thirst.Value=thirst.Value>0 and thirst.Value-1 or 0
        hunger.Value=hunger.Value>0 and hunger.Value-.3 or 0
        hum.Health=thirst.Value<=0 and hum.Health-thirstDamage or hum.Health
        hum.Health=hunger.Value<=0 and hum.Health-hungerDamage or hum.Health
        wait(3)
        enabled=args[1]
    end
end

local function onCharacterAdded(char,thirst,hunger)

    -- reset hunger&thirst
    thirst.Value=starting_thirst
    hunger.Value=starting_hunger

    local hum=char:WaitForChild("Humanoid",6)
    if not hum then return end

    local args={true,char,thirst,hunger,hum}
    coroutine.wrap(drain)(args)
    local co;co=char.Changed:Connect(function()
        if not char.Parent then
            args[1]=false
            co:Disconnect()
        end
    end)
    hum.Died:Wait()
    args[1]=false
    co:Disconnect()
end

local function onPlayerAdded(plr)
    local l=Instance.new("Folder",plr)
    l.Name="PlayerStats"
    local thirst=Instance.new("NumberValue",l)
    local hunger=Instance.new("NumberValue",l)
    thirst.Name="Thirst"
    hunger.Name="Hunger"
    local MaxThirst=Instance.new("NumberValue",l)
    MaxThirst.Value=max_thirst
    local MaxHunger=Instance.new("NumberValue",l)
    MaxHunger.Value=max_hunger
    MaxThirst.Name="MaxThirst"
    MaxHunger.Name="MaxHunger"
-- gui:Clone().Parent=plr.PlayerGui

    plr.CharacterAdded:Connect(function(char)
        onCharacterAdded(char,thirst,hunger)
    end)
    local char=plr.Character
    if char then
        onCharacterAdded(char,thirst,hunger)
    end

end
Players.PlayerAdded:Connect(onPlayerAdded)
for _,v in ipairs(Players:GetPlayers())do
    onPlayerAdded(v)
end

3 comments

0

How difficult is it to stop spoonfeeding? Spoonfeeding does not help the asker learn about what their problem is and how they can resolve their problem in similar scenarios in the future.

0

It worked but id like to know more on how it works etc

0

fix my typo: replace "hunger.Damage" with "hungerDamage" on line 19 or it will error when your hunger reaches 0

This can mean that you are trying to reference something or do something with something that doesn't exist. To solve this, WaitForChild usually helps.

1 comment

0

Already have that

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