How can I fix this script?

Hello, I was recently trying to make a rythm game but this happens when I play:


It keeps on spawning a lot of arrows and chooses the same one over and over again. Here is my coide:

local player = game.Players.LocalPlayer
local uis = game:GetService('UserInputService')
local arrow = game.ReplicatedStorage.Arrow
local Main = script.Parent.Main
local runService = game:GetService('RunService')
local music = workspace["Skeletone Deaf a"]
local tweenTime = 2

local arrowTypes = {
    'UP',
    'Down',
    'Left',
    'Right',
}

function AddArrow(ArrowType)
    if ArrowType == 'UP' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Up.Position.X.Scale
        local posY = script.Parent.Main.Up.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Up.ImageColor3
        clonedArrow.Rotation = 180
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil, tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Down' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Down.Position.X.Scale
        local posY = script.Parent.Main.Down.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Down.ImageColor3
        clonedArrow.Rotation = 0
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil ,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Left' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Left.Position.X.Scale
        local posY = script.Parent.Main.Left.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Left.ImageColor3
        clonedArrow.Rotation = 90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Right' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Right.Position.X.Scale
        local posY = script.Parent.Main.Right.Position.Y.Scale
        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Right.ImageColor3
        clonedArrow.Rotation = -90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    end
end

runService.RenderStepped:Connect(function()
    wait(1)
    local ChosenArrow = math.clamp(music.PlaybackLoudness/20,1,#arrowTypes)
    AddArrow(arrowTypes[ChosenArrow])
end)

Thank you!

1 Like

For the arrows being placed a lot, the issue is that the wait(1) will never work since it’s an event by itself, you need to debounce the event or use a while true do loop like @oskxzr mentioned since those yield on wait()s

And for the chosen arrow only being the right arrow, it’s probably because music.PlaybackLoudness/20 is always return a value higher than 4, meaning it’s always being clamped to 4. Maybe divide it by a higher number than 20?

You’d also have to round the result of music.PlaybackLoudness/20 since if it’s a decimal, it’s going to error when trying to retrieve the arrow

2 Likes

Change

runService.RenderStepped:Connect(function()
    wait(1)
    local ChosenArrow = math.clamp(music.PlaybackLoudness/20,1,#arrowTypes)
    AddArrow(arrowTypes[ChosenArrow])
end)

Do a while loop and add a runService.RenderStepped:Wait() if you want.

2 Likes

This fixes the amount of arrows but it still choosing the same arrow:

local player = game.Players.LocalPlayer
local uis = game:GetService('UserInputService')
local arrow = game.ReplicatedStorage.Arrow
local Main = script.Parent.Main
local runService = game:GetService('RunService')
local music = workspace["Skeletone Deaf a"]
local tweenTime = 2

local arrowTypes = {
    'UP',
    'Down',
    'Left',
    'Right',
}

function AddArrow(ArrowType)
    if ArrowType == 'UP' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Up.Position.X.Scale
        local posY = script.Parent.Main.Up.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Up.ImageColor3
        clonedArrow.Rotation = 180
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil, tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Down' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Down.Position.X.Scale
        local posY = script.Parent.Main.Down.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Down.ImageColor3
        clonedArrow.Rotation = 0
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil ,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Left' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Left.Position.X.Scale
        local posY = script.Parent.Main.Left.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Left.ImageColor3
        clonedArrow.Rotation = 90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Right' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Right.Position.X.Scale
        local posY = script.Parent.Main.Right.Position.Y.Scale
        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Right.ImageColor3
        clonedArrow.Rotation = -90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,tweenTime)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    end
end

while wait(1) do
    local ChosenArrow = math.clamp(music.PlaybackLoudness/4,1,#arrowTypes)
    AddArrow(arrowTypes[ChosenArrow])
end

Thank you!

Divide by a number greater than 4,

Also round the number so it’s never going to be a decimal so it wont error

while wait(1) do
	local num = math.round(music.PlaybackLoudness/35) --replace 35 with a higher number if needed
    local ChosenArrow = math.clamp(num,1,#arrowTypes)
    AddArrow(arrowTypes[ChosenArrow])
end

Thank you very much again! It works like a charm!

1 Like

Anytime! If you have anymore issues don’t be afraid to make antoher post!

1 Like