Need help with script

I’m trying to make this script make it so the coins go up by one every time the tool the script is in hits a part called Stone.

script.Parent.Handle.Touched:Connect(function(hit, Coins)
	if hit:FindFirstChild("Humanoid") == nil then
		if hit ~= nil then
			if hit.name == ("Stone") then
				hit:Destroy()
				local Coins = game.Players.Player("leaderstats").Coins.Value + 1
				Coins
			end

		end
	end
end)

you cant add values while in variables.

local Coins = game.Players.Player("leaderstats").Coins
Coins.Value += 1

Also where is your Player? game.Players.Player("leaderstats") doesnt exists.

1 Like

It still doesn’t work. Its says in the output Player is not a valid member of Players “Players”

  • First, you can use “and” operator to make your code simple.
  • Second, if you want to get the player, you need to use tool.parent and game.Players:GetPlayersFromCharacter(tool.parent)
  • Third, you can’t add values like the script upward.
  • And finally, there is no need to put “Stone” in the bracket

– Edit, I did a wrong mention sorry.

The player variable isn’t defined?

Thats why I asked

Your whole code is wrong.

1 Like

I’m new to scripting, so I have no clue

Can you mention what are you trying to achieve?
like if handle tool touches stone part?

If the handle touches a part called Stone then add 1 to the coins

You can try this code

script.Parent.Handle.Touched:Connect(function(hit, Coins)
	if hit:FindFirstChild("Humanoid") == nil then
		if hit ~= nil then
			if hit.name == ("Stone") then
                local character = script.Parent.Parent
                local player = game.Players:GetPlayerFromCharacter(character)
				hit:Destroy()
				local Coins = player.leaderstats.Coins
				Coins.Value += 1
			end
		end
	end
end)

Getting character by doing script.Parent.Parent
then getting player by using game.Players:GetPlayerFromCharacter(character)

2 Likes

Wait why is there a Coins variable standing alone?

Yeah i forgot to edit that, I just copy paste his code.
and completely forgot about the coins variable.

1 Like

By the way, I don’t think “hit” can be nil, and your code is just too long. You just need to see if the name of the “hit” is “Stone” (since those simple named players don’t usually play games xd)

1 Like

Thanks, I have been trying to do this for an hour now

2 Likes