Can't make part uncollidable

Hello, I’m trying to do a game where you play an obby to get coins and then use the coins to unlock a harder obby, I’m trying to do a part that will become uncollidable upon buying with the coins, but it won’t work, here’s my code:

local price = 3
local db = true
local player = game.Players:GetPlayerFromCharacter(hit.Parent)

script.Parent.Touched:Connect(function(hit)
	if db == true then
		if hit.Parent:FindFirstChild("Humanoid") then
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price
				script.Parent.CanCollide = false
				db = false
			end
		end
	end
end)

Suggest any improvements to my code if you want as well, I’m still new to scripting, thanks.

Shouldn’t you define the player variable with in the Touched function, likely after if hit.Parent:FindFirstChild("Humanoid") then?

local price = 3
local db = true

script.Parent.Touched:Connect(function(hit)
	if db == true then
		if hit.Parent:FindFirstChild("Humanoid") then
		     local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- this is where it should be
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price
				script.Parent.CanCollide = false
				db = false
			end
		end
	end
end)

line 3: local player = game.Players:GetPlayerFromCharacter(hit.Parent)
you called the “hit”(value) before it was even define(or made).

It still doesn’t work, I had my code like that before but then thought that I had to put all the variables at the start, anyways, do you know anything else that may be the problem? This is the part structure if that may help:
image

weird, i just tested it and it works. can you send me a video of you touching the coin?

Here video of me touching the coin
robloxapp-20211229-1906315.wmv (513.2 KB)

No error or issue what so ever,

It wasn’t supposed to be a coin, I want to make a part (kind of like a door) that dissapears when you buy with the in game coins (which I already made)

Did you try finding the location of the problem using print() and seeing where it wouldn’t run?

There could possibly be an error where the script cannot find the coin or cash value in leaderstats.

How do I do that exactly? Could you tell me where and what to put?

The coins are called Coins in the leaderboard and I put it like that in the code

basically try using print() after every line having each of them say something different, then find where it stopped printing

The last print was of this line, didn’t get any print output after that

I think its the if statement that is causing the problem.

Hi, use this code and make a remoteEvent in ReplicatedStorage and name it: CoinTransparencyModifier
then put this script in your Coins or Parts:

local MemoryBank = {}; – this is a table. you can store vales in here with it.
local CoinValue = 100; – the amount that the coin is worth.
local WaitTime = 5; – Seconds. The cooldown time for the coin.
local Coin = script.Parent; – The coin
local ReplicatedStorage = game:GetService(“ReplicatedStorage”); – this gets the replicated storage
local Players = game:GetService(“Players”); – this gets the players
local CoinTransparencyEvent = ReplicatedStorage:WaitForChild(“CoinTransparencyModifier”); --remote event. Passes infomation to client

local function OnTouch(Hit)
if Hit.Parent:FindFirstChildWhichIsA(“Humanoid”) then
local Player = Players:GetPlayerFromCharacter(Hit.Parent);
local CoinsValue = Player:WaitForChild(“leaderstats”):WaitForChild(“yourCurrencyHere”);

	if not table.find(MemoryBank,Player) then
		CoinsValue.Value += CoinValue;
		table.insert(MemoryBank,Player);
		CoinTransparencyEvent:FireClient(Player,Coin, false, 1);
		wait(WaitTime);

		pcall(function()
			local Num = table.find(MemoryBank, Player);
			table.remove(MemoryBank,Num);
			CoinTransparencyEvent:FireClient(Player,Coin, false, 0);

		end)
	end
end

end
Coin.Touched:Connect(OnTouch)


This is a script when you touch a part its make transparent and not collide and when you collect them, the another player can collect it too.

And make a line how make the part CanTouch = false, then CanTouch = true

Sorry for my english. Happy new year.

Im trying to make a part that can be bought WITH the coins and when you buy it you should be able to trespass it and it becomes invisible

Alright, just to make sure of where the problem is, could you try doing print(player) after local player = game.Players:GetPlayerFromCharacter(hit.Parent) (if it returns nil then that means that’s where the problem is)

like a door in a simulator, when you buy it its transparence?

Exactly! That’s what I’m trying to achieve

It prints my name, as I said, anything before the line I mentioned prints an output (so I guess they’re working up until that point)