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 part that, when touched, adds a point to Points. Can yall help me?

script.Parent.Touched:Connect(function(plr)
	local pp = plr.leaderstats.Points
	pp.Value = pp.Value + 1
end)

Parent is a part.

1 answer

You can't get a player from a touched event, it only checks for the object that's touching the part (in which the touch event script is located in). And so because of this, you need to make use of GetPlayerFromCharacter()

This is the code with GetPlayerFromCharacter():

local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
	if hit then
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		if Humanoid and Humanoid ~= nil then
			local Player = Players:GetPlayerFromCharacter(hit.Parent)
			local Points = Player.leaderstats.Points
			Points.Value = Points.Value + 1
		end
	end
end)

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