Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How should i fix this piece of code that reapeats?

Asked by 1 year ago

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

0
By continuously, do you mean like a while loops or more than once? deeskaalstickman649 475 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

It looks like you are not using a debounce/cooldown for the event. Here is an example for a debounce in a touched event:

local debounce = false

script.Parent.Touched:Connect(function()
    if debounce == false then
        debounce = true
        -- Do script stuff here
    else
        wait(3) -- Cooldown
        debounce = false
    end
end
Ad
Log in to vote
1
Answered by 1 year ago

.Touched events trigger the code below them once, its not like a while condition. on line 40 where it says:

for i = 1, math.huge do

you SHOULD NOT use math.huge that makes it so that when the player touches the thing it will trigger and that will actually get stuck in a loop effectively forever, billions of years. instead of using math.huge in a for loop taking the player's money constantly, I would use a while loop. It would read something like:

While Touched and Debounce do
    Debounce = false
    --take money away
    wait(interval for money stealing)
    Debounce = true
end

and within the event where the touch end is detected just write:

Touched = false

and in the one where the touch is detected say:

Touched = true

dont forget to define Touched and Debounce at the top of the code

0
yeah i made a sort of mistake there Boyuanbogosess 17 — 1y

Answer this question