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

How do i make a brick increase a stat, but only when a player is touching the brick?

and when the player steps off the brick the stat goes back to normal

2 answers

Hello, diamondkfc!

This script detects player Touch(When it starts and when it ends), stores in a var the state and then tests

local Touching = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --Tests if is player
		Touching = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if Touching then --Tests if it was touching
		Touching = false
	end
end)

while wait(.1) do
	if Touching then
		--Change the brick stat here
	else
		--Back the brick stat to normal here
	end
end

Hope this helps

Scripting helpers is not a request website

Good Luck with your games

Assuming that you mean the statistics in the ROBLOX default leaderboard and that you mean to repeatedly increase the stat:

-- Script inside the brick
-- Note: This script is made by yankana and it is untested!

local statName = "Stat Name Here"
local increment = 1

local players = game:FindServce("Players")
local brick = script.Parent
local activePlayers = {} -- 1

brick.Touched:Connect(function(hit)
	local player = players:GetPlayerFromCharacter(hit.Parent) -- 2
	if player and not activatePlayers[player] then -- 3
		activatePlayers[player] = true
		local stat = player.leaderstats[statName]
		local char = player.Character:GetChildren()
		while true do -- 4
			local touching = false -- 5
			for _, v in pairs(char) do -- 6
				if touching or not v.Parent then -- 7
					break
				end
				if v:IsA("BasePart") then -- 8
					for _, v2 in pairs(v:GetTouchingParts()) do -- 9
						if v2 == brick then
							touching = true
							break
						end
					end
				end
			end
			if not touching then -- 10
				activatePlayers[player] = false
				break
			end
			stat.Value = stat.Value + increment
			wait() -- 11
		end
	end
end)

A few notes to what I've done:

  1. I first made a table that we would use in the script to put in players that have touched the brick, and then when they touch it again, check if they're already in the table and deny doing double-increments.
  2. I've used the GetPlayerFromCharacter method for better compatibility.
  3. I check if there was a player returned and that they don't already exist in the table.
  4. I start an unending loop (until/unless broken).
  5. I make a local variable (set to false) that indicates whether the player is currently touching the brick or not. It starts at false and will become true later if the script detects that one of the character's parts is touching the brick.
  6. I loop through the children of the player's character and check if any of them is currently touching the brick.
  7. I check if touching is already true, or if the part doesn't have a parent, to then break the loop to take a different action.
  8. I check if the instance is a BasePart (a physical object, and not something like a script, because then it would error trying to call GetTouchingParts on it).
  9. I call GetTouchingParts on the part to see if any of the touching parts is the brick.
  10. Finally, I break the loop if none of the parts are touching the brick, and remove the player from the activePlayers table. Otherwise, I increment the stat and continue the loop from the start again.
  11. Let's not forget to add a wait so the loop doesn't freeze. (It waits close to 1/30th of a second during each step of the loop.)

If you have any issues or questions about this script then don't hesitate to let me know.

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