and when the player steps off the brick the stat goes back to normal
COMMUNITY QUESTION
How do i make a brick increase a stat, but only when a player is touching the brick?
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:
- 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.
- I've used the GetPlayerFromCharacter method for better compatibility.
- I check if there was a player returned and that they don't already exist in the table.
- I start an unending loop (until/unless broken).
- 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.
- I loop through the children of the player's character and check if any of them is currently touching the brick.
- I check if
touchingis already true, or if the part doesn't have a parent, to then break the loop to take a different action. - 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).
- I call GetTouchingParts on the part to see if any of the touching parts is the brick.
- Finally, I break the loop if none of the parts are touching the brick, and remove the player from the
activePlayerstable. Otherwise, I increment the stat and continue the loop from the start again. - 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.
