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)
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)
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.
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.
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)
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.
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)
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)