Script deals 2x as much damage on second use

i have a script where it deals 25 damage
when i use it again it deals 50 damage??!?
pls help

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local selected = false
local selectednpc = nil
local currentnpc = nil

function leftClick()
if selected == false then
script.Parent.Text = (“Who do you wanna attack bro?”)
selected = true
end
if selected == true then
mouse.Button1Down:Connect(function()
if mouse.Target.Parent.Name == (“badguy”) then
currentnpc = mouse.Target.Parent
selectednpc = mouse.Target.Parent.Humanoid
player.Character.Humanoid:MoveTo(mouse.Target.Parent.HumanoidRootPart.Position)
wait(2)
local Attack = player.Character.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Attack)
Attack:Play()
wait(0.6)
local damagedui = game.ReplicatedStorage.damage1:Clone()
damagedui.Parent = currentnpc.Head
selectednpc:TakeDamage(25)
wait(1)
currentnpc = nil
selectednpc = nil
player.Character.Humanoid:MoveTo(game.Workspace.battleset.battlepos.Position)
wait(1.9)
script.Parent.Text = (“attack”)
selected = false
end
end)
end
end
script.Parent.MouseButton1Click:Connect(leftClick)

It’s a little hard to see with the way the code is formatted, but it looks like you are wrapping event listeners inside one another, meaning the ‘attack’ code might be running an additional time after each use.

how would i fix dis

30characters

Switch the position of if selected == true then to inside the Button1Down

Instead you do


mouse.Button1Down:Connect(function()
if selected == true then
1 Like