A clicking game with leaderstats?

Hi,

How do I make it so that if a part is touched, it adds something to leaderstats? How do I begin with this?

I have this

local part = workspace.Block
local Player = game:GetService("Player")

part.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		Player.leaderstats.Touches.Value = 10
	end
end)
1 Like

I get this error:

GetPlayerFromCharacter returns the player from the character model you put in it’s arguments

local Players = game:GetService("Players")

local part = workspace.Block

part.Touched:Connect(function(hit)
    local model = hit:FindFirstAncestorOfClass("Model") --make sure to find a character model from the hit object
    if not model then return end --if there's no model parent then do nothing and return

    local player = Players:GetPlayerFromCharacter(model) --if there is a model then try to get player with it

	if player then --check if that getplayerfromcharacter function returned the player associated with the model
		player.leaderstats.Touches.Value += 10 --do something
	end
end)
1 Like

Add an “s” at Player. Not at your local but at ("Player")

So your code should be:

local part = workspace.Block
local Player = game:GetService("Players")

part.Touched:Connect(function(hit)
 if game.Players:GetPlayerFromCharacter(hit.Parent) then
  Player.leaderstats.Touched.Value = Player.leaderstats.Touched.Value + 10
 end
end)

And yes, I typed that with my hands that took about 2 or 4 minutes to type that entirely.

1 Like

Hi, thanks for that! I’m not sure why you added local model and if not model. Is that part necessary?

Thanks!

But, I do get this error:

leaderstats is not a valid member of Players "Players"

If you read the comments, i added the if not model then return end line so that if we don’t get a character model ancestor, we simply do nothing since that cannot be a player’s character.

What’s your folder’s name ?

This text will be blurred

The issue is that your trying to get the leaderstats of a service called “Player” (also Player is not even a service, you might mean the service “Players”).

Try somthing like this:

local Part = workspace.Part
local Players = game:GetService("Players")

Part.Touched:Connect(function(PartTouched)
	if PartTouched.Parent:FindFirstChild("Humanoid") then
		if Players:FindFirstChild(PartTouched.Parent.Name) then
			Players:FindFirstChild(PartTouched.Parent.Name).leaderstats.Touches.Value += 10
		end
	end
end)
1 Like

Rather then getting the value and then adding 10 to it you can just use += 10 that will do the exact same.

Hi!

It works, but it’s not a hit function. I want the player to touch it through clicking on it.

What I would recommend to you then is add a click detector inside and then reference the click detector and link the function up with the . MouseClick event.

I changed it.

Is this right?

local Part = workspace.Block
local clickDetector = workspace.Block.ClickDetector
local Players = game:GetService("Players")

function onMouseClick()
			Players:FindFirstChild(onMouseClick().Parent.Name).leaderstats.Touches.Value += 10
		end
	end
end)

clickDetector.MouseClick:Connect(onMouseClick)

Your script will always error if you do 3 ends after function onMouseClick().

local Part = workspace.Block
local clickDetector = workspace.Block.ClickDetector
local Players = game:GetService("Players")

function onMouseClick()
	Players:FindFirstChild(onMouseClick().Parent.Name).leaderstats.Touches.Value += 10
end)

clickDetector.MouseClick:Connect(onMouseClick)
1 Like

Thank you, that makes sense, but im getting a syntax error.

1 Like

Remove ) at end). But is it the issue of the leaderstat bug again?

Change the onMouseClick().Parent.Name to something like below code:

function onMouseClick(plr)

plr.leaderstats.Touches.Value += 10

end

clickDetector.MouseClick:Connect(onMouseClick)
1 Like

It’s not saving either sadly :confused:

How about that?

local Part = workspace.Block
local clickDetector = workspace.Block.ClickDetector
local Players = game:GetService("Players")

function onMouseClick()
	Players:FindFirstChild(onMouseClick().Parent.Name).leaderstats.Touches.Value += 10
end

clickDetector.MouseClick:Connect(onMouseClick)

I seem to be getting lag when I click the part. Any reason why?
Screenshot 2022-10-19 at 14.43.48