Part that gives a coin

So I need a part to give a coin when the player touches it, but the part is with the CanCollide off so the player can walk throw it. But with this script it doesent work. So, how can I make a part give a coin when touched eve if the CanCollide is off?
The script:

local part = script.Parent

local function onTouch(other)
    local character = other.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
        local coins = player:GetAttribute("Coins") or 0
        player:SetAttribute("Coins", coins + 1)
    end
end

part.Touched:Connect(onTouch)

Make the coin system with leaderstats or make a folder inside the player, then do it again but with number value

You could do like Sparsrsa said, a script inside ServerScriptService that create a IntValue inside the Player or inside a Folder that is inside the Player.

game.Players.PlayerAdded:Connect(function(Player) --When the Player join, this script will run.
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0 --Put here the value you want to start with
	Coins.Parent = Player
end)

Or

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "Leaderstats"
	Leaderstats.Parent = Player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = Leaderstats
end)

If you do that, you just need to change that value to +1 (Or the amount you want the coin to give)

Here the script.

script.Parent.Touched:Connect(function(Hit) --I like to use "Hit", but you don't need to.
	if Hit.Parent:FindFirstChildWhichIsA("Humanoid") then --Detecting if it's a Player.
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		local Coins = Player:WaitForChild("Coins") --Or Player:WaitForChild("Leaderstats"):WaitForChild("Coins")
		Coins.Value = Coins.Value + 1 --Put here the amount you want.
	end
end)
2 Likes

The Touched event does not depend of the CanCollide property, but of the CanTouch property.

3 Likes

check if the CanTouch property is enabled

1 Like

As GoulStorm mentioned, the CanCollide property does not affect when the .touched event fires, it depends on if the CanTouch property is enabled. The CanTouch property is a property found below the CanCollide property than makes it so that the part registers when it is touched. So if the CanTouch property was off and CanCollide was on, there would be no event. So make sure your CanTouch property is on and that your script works without errors.

2 Likes