Roblox Script Count anytime Player touches a Part

Posted on May 1, 2020

Roblox Script Count anytime Player touches a Part

I'm extending the sample you may find in the reference of the Player here.

The sample script allows to create a Folder with a numeric Value for each new Player that comes into the game.

Adding a Part in the game with this code attached, we can keep in memory the number of touches the Player does with the Part.

This assumes the Folder is named "MyFold" and the Value "MyScore":

local function onTouch(part)
    local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
    if player then
        local score = player.MyFold.MyScore
        score.Value = score.Value + 1


This is the code of the Main script that comes from the mentioned sample:

local Players = game:GetService("Players")

local function onAdded(player)
    local fold ="Folder")
    fold.Name = "MyFold"
    local score ="IntValue")
    score.Name = "MyScore"
    score.Value = 0
    score.Parent = fold
    fold.Parent = player