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.
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:
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)
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
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)