How do you give players a tool if they have a certain amount of points if they touch a part?

I attempted this by using a script but doesn’t work and no errors.

local Part = script.Parent
local A1Card = game.ReplicatedStorage.A1Card
local debounce = false
function onTouched(p)
	local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	local leaderstats = player:FindFirstChild("leaderstats")
	local Coins = leaderstats:FindFirstChild("Gold")
	if p.Parent == player then
		if Coins then
			if Coins.Value > 299 then
				Coins.Value = Coins.Value - 300
				A1Card:Clone(player.Starterpack)
			end
		end
	end
end
script.Parent.Touched:connect(onTouched)
if Coins.Value > 299 then
	Coins.Value = Coins.Value - 300
	A1Card:Clone(player.Starterpack)
end

You only cloned the tool, you didn’t parent it to the player.Backpack. What you should do is change this:

A1Card:Clone(player.Starterpack)

to this:

local NewA1Card = A1Card:Clone()
NewA1Card.Parent = player.Backpack

1 Like

there is a problem with

if p.Parent == player then

I think because it isn’t even changing the coins value

In the code you gave, player is the Player instance, not the player’s character. You can see this from player = game.Players:GetPlayerFromCharacter(humanoid.Parent), because :GetPlayerFromCharacter returns the Player instance associated with the player character. If you wanted the character, you would just use humanoid.Parent.

Also I strongly suggest adding a check that humanoid is not nil under the local humanoid = ..., because otherwise the game will break if anything other than part of the player’s character touches the part.

1 Like