Why does this code not work? please help

so basically what this script/code does is if the player touched the part then it will check to see if the player has enough regen pixles and if it does or more then it will give the player 1 regen and it will reset the regen pixles. Kinda like rebirth simulator.

here is the script/code:

local function regen1(hit)
	local player = hit.Parent
	local stats = player:FindFirstChild("leaderstats")
	local regen = stats.regen.Value
	local regenP = stats["regen pixle"].Value
	if regenP.Value >= 5 then
		regen.Value = regen.Value + 1
		regenP.Value = 0
		wait(0.1)
	end
	
end

script.Parent.Touched:Connect(regen1)

I believe when the player touches the part it’s actually getting the character as the parent of hit. So, in order to get the player, I would add the lines:

local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
1 Like

so the regen variable is the stats.regen.Value, but then you say regen.Value = regen.Value + 1. This is like saying stats.regen.Value.Value = stats.regen.Value.Value + 1 which doesn’t work. Instead try this:

local function regen1(hit)
	local player = hit.Parent
	local stats = player:FindFirstChild("leaderstats")
	local regen = stats.regen
	local regenP = stats["regen pixle"]
	if regenP.Value >= 5 then
                print("regen")
		regen.Value = regen.Value + 1
		regenP.Value = 0
		wait(0.1)
	end
	
end

script.Parent.Touched:Connect(regen1)