so I have this npc that goes to the player at a certain range and it steals the players money. Its working fine but the issue is that when it steals money it steals it continuously so you have a negative value of money.
Here is the code:
--Note that this code wont work on multiplayer
local players = game.Players
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")
local human = script.Parent:WaitForChild("Humanoid")
local ZombieTorso = script.Parent:WaitForChild("Torso")
local CashParticle = script.Parent.Vacuum.CashParticle.CashParticle
local VacuumSFX = CashParticle.Parent.VacuumSFX
local cashGrabAmount = script.Parent.CashGrabAmount
-----Money stealing ------
local cashGrabDetector = script.Parent.GrabCashDetector
players.PlayerAdded:Connect(function(player)
--Holding Anim
local holdingVacuum = script.Parent.HoldingVacuum
local Controller = script.Parent.Humanoid
Controller:LoadAnimation(holdingVacuum):Play()
cashGrabDetector.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("CashDisplayValue") then
local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out, 1, false, 0)
local VacuumHead = script.Parent.Vacuum.VacuumHead
local GrowingSize = {Size = Vector3.new(4, 3, 4)} --2.151, 1.429, 2.151 Normal size
local tween = TweenService:Create(VacuumHead, tweenInfo, GrowingSize)
tween:Play()
wait(1)
VacuumSFX:Play()
CashParticle.Enabled = true
script.Parent.Head.CashGUI.CashText.Text = "-$" .. cashGrabAmount.Value
script.Parent.Head.CashGUI.Enabled = true
script.Parent.Head.Cash:Play()
wait(1.45)
script.Parent.Head.CashGUI.Enabled = false
for i = 1, math.huge do
wait(1)
cashGrabDetector.CanTouch = false
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cashGrabAmount.Value
wait(1)
cashGrabDetector.CanTouch = false
end
end
if player.leaderstats.Cash.Value == 0 then
wait(1)
cashGrabDetector.CanTouch = false
human:MoveTo(ZombieTorso.Position + Vector3.new(math.random(-240,240) , 0, math.random(-112,112)))
elseif player.leaderstats.Cash.Value >= cashGrabAmount.Value then
wait(1)
cashGrabDetector.CanTouch = true
end
end)
cashGrabDetector.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("CashDisplayValue") then
local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 1, false, 0)
local VacuumHead = script.Parent.Vacuum.VacuumHead
local ShrinkingSize = {Size = Vector3.new(2.151, 1.429, 2.151)} --2.151, 1.429, 2.151 Normal size
local tween = TweenService:Create(VacuumHead, tweenInfo, ShrinkingSize)
tween:Play()
wait(1)
VacuumSFX:Stop()
CashParticle.Enabled = false
end
end)
-----AI
local maxDistance = 100
local character = player.Character or player.CharacterAdded:Wait()
while wait(2.3) do
if (ZombieTorso.Position - character.Torso.Position).Magnitude <= maxDistance then
local path = PathfindingService:CreatePath()
path:ComputeAsync(ZombieTorso.Position, character.Torso.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait()
end
human:MoveTo(character.Torso.Position)
elseif (ZombieTorso.Position - character.Torso.Position).Magnitude > maxDistance then
wait(1)
human:MoveTo(ZombieTorso.Position + Vector3.new(math.random(-240,240) , 0, math.random(-112,112)))
end
end
end)
the issue here is from line 22 to 38

1 comment
By continuously, do you mean like a while loops or more than once?