Cannot get money when touching part

function onHit(player)
	if player then
		player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value + 100
	end
end
onHit()

Here is my leaderboard script:

	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Pets = Instance.new("IntValue")
	Pets.Parent = leaderstats
	Pets.Name = "Pets"
	Pets.Value = 1

	local Bucks = Instance.new("IntValue")
	Bucks.Parent = leaderstats
	Bucks.Name = "Bucks"
	Bucks.Value = 0
end)

Is there a reason why it is not increasing when I touch it?

script.Parent.Touched:Connect(function(Hit))
    if Hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild("Hit.Parent.Name") then
        local Player = game.Players:FindFirstChild("Hit.Parent.Name")
        local leader = Player:FindFirstChild("leaderstats")
        if leader ~= nil then
           local Bucks = leader:FindFirstChild("Bucks")
           if Bucks ~= nil then
               Bucks.Value = Bucks.Value + 100
           end
       end
    end
end)

Is this your whole script under the part? If so, you’ll want to add a .Touched event. For example:

script.Parent.Touched:Connect(onHit)

leaderstats is not a valid member of MeshPart “Workspace.jumbopushpop112.LeftFoot” is the error I am getting in the console

.Touched events give the part that hit as the parameter, not the player. If you look at @Crygen54’s response, you can see an example of how to get the player there.

Touched passes the part that touched this part. You need to first check and see if it belongs to a character, if it does then you check if that character belongs to a player.

if hit.Parent:FindFirstChild('Humanoid') then -- check if char
    local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)

    if player then
        -- touched by a players character
        player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value + 100
    end
end
1 Like