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

My code to run a script while a player is touching it, is running the code several times?

Asked by 2 years ago
Edited 2 years ago
local CashButtonsFolder = game.Workspace.CashButtons
local Players = game:GetService("Players")

game.ServerScriptService:WaitForChild("Values")
Players.PlayerAdded:Connect(function(player)
    player:WaitForChild("playerstats")
    for _, v in pairs(CashButtonsFolder:GetChildren()) do   
        if v:IsA("BasePart") then
            v.Touched:Connect(function(touching)
                task.wait()
                local player = Players:GetPlayerFromCharacter(touching.Parent)
                local Cash = player.playerstats.Cash
                local Multiplier = player.playerstats.Multiplier
                local Rebirth = player.playerstats.Rebirths
                local Metal = player.playerstats.Metal
                local Cost = v.Cost
                local Multi = v.Multi
                local NewValue
                local CashVal

                local MetalMultiplier = Metal.Value/2 + 1 -- 1 metal = 1.5x multiplier, 2 metal = 2x multiplier, etc.

                db = true
                while db do
                    task.wait()
                    local humanoid = touching.Parent:FindFirstChild("Humanoid")
                    if db then
                        db = false
                        if humanoid and humanoid.MoveDirection == Vector3.new(0, 0, 0) and Cash.Value >= Cost.Value then
                            db = false
                                db = false
                                    CashVal = Cash.Value - Cost.Value
                                    Cash.Value = CashVal
                                    print("detected")
                                    NewValue = Multiplier.Value + Multi.Value * (Rebirth.Value + 1) * MetalMultiplier
                                    Multiplier.Value = NewValue
                                    wait(1)
                                    db = true
                        end
                    end
                end
            end)
        end
    end
end)

Please help, I don't know what to do

Player is supposed to be touching a button, and while they touch that button they will gain stats accordingly, but I want it to be in intervals so it's not all at once.

0
I've tried manipulating the DB in every way i can think of, that's why it looks like that AspektZero 0 — 2y

Answer this question