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

?Trying to make a script that when you are touching the part you gain time but stops when u get off

Script in part

local interval = 1
local ting = 0

function onTouched(hit)
	
	local lol = hit.Parent:FindFirstChild("Touched")
	lol.Value = 0
	if lol.Value == 1 then
		wait()
	else
		if ting == 0 then
			ting = 1 
			local check = hit.Parent:FindFirstChild("Humanoid")
			
			if check ~= nil then
				local CoolBeans = 1
				while CoolBeans == 1 do
					for i,v in pairs(game.Players:GetPlayers())do
						v.leaderstats.Time.Value = v.leaderstats.Time.Value + 1
						wait(interval)
						if lol.Value == 0
							
							then local Coolbeans = 0
						
						end
					end
				end
			end
		end
		end

	ting = 0
	end

	script.Parent.Touched:connect(onTouched)

Leaderboard script

function PlayerEntered(newPlayer)
local stats = Instance.new("IntValue")
stats.Parent = newPlayer
stats.Name = "leaderstats" --the box that shows up on the screen below is what it will have in it
	
local Time = Instance.new("IntValue")
Time.Parent = stats
Time.Name = "Time"  --what you want youre currency to be called ie (robux, tix ect.) if you edit this make sure you make two if these"" around the word or it wont work
Time.Value = 0  --how much money or in this case points you want youre map to start with
	
	local Meme = Instance.new("IntValue")
	Meme.Value = 0
	Meme.Name = "Touched"
	
end

game.Players.ChildAdded:connect(PlayerEntered)

I'm not very good at scripting but I really want to get this to work but I just can't figure it out. The script is probably all messed up because I suck at scripting but if you have any question put them in the comments.

1 answer

For the leaderboard, you need to create a 'folder' name "leaderstats", not an IntValue name leaderstats:

local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = newPlayer

For the script:

local debounce = false
script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and humanoid ~= nil then -- check if it is the player
		if debounce == false then  -- we don't want the player to touch it 100 times in 1 second
			debounce = true  -- so that the player can't touch it ealier
			wait(1) -- Amount of time the player needs to wait before touching the part again, you could change this if you want
			local name = game.Players:GetPlayerFromCharacter(hit.Parent)
			if  name then   -- check if the player is on the leaderboard
				name.leaderstats.Time = name.leaderstats.Time + 1
			end
			debounce = false -- allows the player to touch it again
		end
	end
end)

I've done this because you can only find the leaderstats folder with the intvalue in it through the Players section that has the same parent as the Workspace. That's why you can't access the IntValue thing name "Touched" through the player's model in the workspace.

hit.Parent:FindFirstChild("Touched")  -- since there is no touch in the player model, the script will skip.

for i,v in pairs(game.Players:GetPlayers())do -- this could be all player in the server

If there is any error or any question you would like to ask, you can ask.

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