I’m not sure what’s wrong with this script, but this is a script that detects when a card touches a scanner, the value of the player’s cash (in leaderstats this is the currency BC$) to deplete by BC$ 2. This script doesn’t seem to work and I’m not sure why, it might be because of the Touched function or trying to find the player from their workspace instance to their Players service instance. Any ideas?
script.Parent.Touched:Connect(function(part)
if part.Name == "card" then
local name = part.Parent.Parent.Name
local plr = game.Players:FindFirstChild(name)
local ls = plr:WaitForChild("leaderstats")
ls["BC$"].Value = ls["BC$"].Value - 2
end
end)
I assume this is supposed to happen when the player equips that card, so -
script.Parent.Touched:Connect(function(Object)
local isModel = Object:FindFirstAncestorWhichIsA("Model")
local isHum = isModel:FindFirstChild("Humanoid")
if Object.Parent.Name == "card" then -- the tool's name
local plr = game.Players:FindFirstChild(isModel)
if plr then
plr.leaderstats["BC$"].Value -= 2
end
end
end)
Try this.
Explanation:
We’re checking if the object that touched the scanner is a descendant of a model[the character]
We’re also checking if that ancestor[the character] has a humanoid, if it does,
we then check the name of the card,[ the tool], and continue from there.
I also suggest putting a debounce, so that - it won’t subtract 2 BC$ from them too quickly.
Before I try fixing, here are some tips you can use:
Make sure you check output for any errors
Lua is case sensitive
Leaderstats can only be set from the server
Now, let’s try to fix this…
script.Parent.Touched:Connect(function(part)
if part.Name == "card" then
if part.Parent:FindFirstChild("Humanoid") then
local character = part.Parent
local name = character.Name
local plr = game.Players:WaitForChild(name)
local ls = plr:WaitForChild("leaderstats")
ls["BC$"].Value -= 2
end
end
end)
let’s see. I will turn CanCollide on on all parts… update when I’m done… I’ve checked everything and everything is already CanCollide on, maybe the problem is that it’s a local script?
[Fixed myself]
The problem was the script was a LocalScript not a server Script. Thanks for everyone trying to help me fix it but it was just a careless mistake.