Add stat when player touched part

hewo, i want to know if that when a player touched a Part, it add 1 to player’s stats, i am fairly new at scripting, no idea how adding stat works, heres what i tried:

function Onplayerentered(player)

local leaderstats = Instance.new(“IntValue”)

leaderstats.Parent = player

leaderstats.Value = 0

leaderstats.Name = “leaderstats”

local stat = Instance.new(“IntValue”)

stat.Name = “Block” – Put name here in between the quotations

stat.Value = 0-- Put the starting Value#

end

game:GetService(“Players”).ChildAdded:Connect(Onplayerentered)

local part = game.workspace.Part

part.Touched:Connect(function(hit)

leaderstats.Value + 1

end)

1 Like
function Onplayerentered(player)

local leaderstats = Instance.new(“IntValue”)

leaderstats.Parent = player

leaderstats.Value = 0

leaderstats.Name = “leaderstats”

local stat = Instance.new(“IntValue”)

stat.Name = “Block” – Put name here in between the quotations

stat.Value = 0-- Put the starting Value#

stat.Parent = leaderstats

end

game:GetService(“Players”).PlayerAdded:Connect(Onplayerentered)

local part = game.workspace.Part

part.Touched:Connect(function(hit)

leaderstats.Block.Value += 1

end)
1 Like

hmm… does this add on ALL players or the one who touched the brick?

function Onplayerentered(player)
	leaderstats = Instance.new("IntValue")
	leaderstats.Parent = player
	leaderstats.Value = 0
	leaderstats.Name = "leaderstats"

	local stat = Instance.new("IntValue")
	stat.Name = "Block"
	stat.Value = 0

end

game:GetService("Players").ChildAdded:Connect(Onplayerentered)

local part = game.workspace.Part

part.Touched:Connect(function(hit)
	leaderstats.Value += 1
end)
  • Make leaderstats a Folder instead of an IntValue
  • Make stat a child of leaderstats by doing stat.Parent = leaderstats
  • Replace game:GetService(“Players”).ChildAdded:Connect(Onplayerentered) with game:GetService(“Players”).PlayerAdded:Connect(Onplayerentered)

Instead of doing:
leaderstats.Value + 1

Do:
stat.Value = stat.Value + 1

Here is the full fixed script:

function Onplayerentered(player)
    local leaderstats = Instance.new("Folder") -- Change to folder
	leaderstats.Parent = player
    leaderstats.Value = 0
	leaderstats.Name = "leaderstats"

	local stat = Instance.new("IntValue")
	stat.Name = "Block"
	stat.Value = 0
	stat.Parent = leaderstats -- Place stat inside leaderstats
end

game:GetService("Players").PlayerAdded:Connect(Onplayerentered)


local part = game.workspace.Part

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Checks if the touch is a character model.
    	local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Grabs player.
    	local playerStatFolder = player:WaitForChild("leaderstats") -- Identifies the player's leaderstats.
    	local stat = playerStatFolder:WaitForChild("Block") -- Identifies the player's block stat.

    	stat.Value = stat.Value + 1
    end
end)

Note: this only applies to the player who touches the part.