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

My updating variable every second script doesn't work, can you help?

Hi there! I'm making a game where the players' points update by one every second. I need the points to start updating only when the player steps on a model, called "Game." Here is my script, which doesn't work. I hvae tried many times to fix this, but all attempts were unsuccessfull. (I would have added those attempts, but I delete them XD) Can you help me?

local Players = game:GetService("Players")
	local UpdatePoints = false
	local function onTouch(part)
	    return end
	end
	part.Touched:Connect(onTouch) 
	Players.PlayerAdded:Connect(function(Player)   
		    Player.CharacterAdded:Connect(function(Character)
			        UpdatePoints = true
			 
			        local Leaderstats = Instance.new("Folder")
			        Leaderstats.Name = "leaderstats"
			        Leaderstats.Parent = Player
			 
			        local Points = Instance.new("IntValue")
			        Points.Name = "Points"
			        Points.Parent = Leaderstats
			 
			        Player.leaderstats.Points.Value = 0
			 
			        local Humanoid = Character.Humanoid
		        Humanoid.Died:Connect(function()
				            UpdatePoints = false
				            Player.leaderstats.Points.Value = 0
			        end)
			 
			        while (UpdatePoints) do
				            wait(1)
			            Player.leaderstats.Points.Value += 1
				            if not (UpdatePoints) then
					                break
				            end
				        end
			    end)
end)

Also, where would I put this script, if you can fix it. Thank you!

1 comment

0

first of all, format your script!! i cant read this

1 answer

Hey Pug,

I saw a few things wrong with your script and I think fixed all the issues you were having. First, I moved the variable inside of the PlayerAdded event, because I think you only wanted to change the player's points depending on their variable bool value. Second, I moved the creation of stats outside of the CharacterAdded event as that will just add more and more leaderstats folders as they die or whenever it's added. Although, I did keep the HumanoidDied event inside of there.

local Players = game:GetService("Players")

--[[
local function onTouch(part)
	return 
end

part.Touched:Connect(onTouch)]]

Players.PlayerAdded:Connect(function(Player)   
	local UpdatePoints = true
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = 0
	Points.Parent = Leaderstats
	
	Player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			UpdatePoints = false
			Points.Value = 0
		end)
	end)
	
	while wait(1) do
		if (UpdatePoints) then
			Points.Value += 1
		end
	end
end)

1 comment

0

Where do I put this?

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