I have a script where you click, the bar charges then you click again then release a ball. It works the first time, but the second time when you click to charge the ball just releases without charging. Why is this happening?
Local Script:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Tool = script.Parent.Parent
local GUI = Tool:WaitForChild("PowerGui")
if not Character.Parent then
Character.AncestryChanged:Wait()
end
local Root = Character:WaitForChild("HumanoidRootPart")
local Hum = Character:WaitForChild("Humanoid")
local Throw = script:WaitForChild("Throw")
local Mouse = Player:GetMouse()
Mouse.TargetFilter = workspace
local Head = Character:WaitForChild("Head")
local PB = GUI:WaitForChild("PowerBar")
local B = PB:WaitForChild("Bar")
local Line = B:WaitForChild("Line")
local Remotes = game.ReplicatedStorage:WaitForChild("Remotes")
local CM = Remotes:WaitForChild("LocalMechanics")
local power = 1
local MAX = 100
local releasedClick
local Hold = script:WaitForChild("HoldIdle")
local HoldTrack = Hum:LoadAnimation(Hold)
local PlayingHold
local equipped
function release()
equipped=nil
HoldTrack:Stop()
game.Debris:AddItem(GUI, 3)
releasedClick=true
local ThrowTrack = Hum:LoadAnimation(Throw)
ThrowTrack:Play()
local T
T = ThrowTrack:GetMarkerReachedSignal("Throw"):Connect(function()
print'running'
Mouse.TargetFilter = workspace
local origin = Head.CFrame.p
local target = Mouse.Hit.p
CM:FireServer("Throw", {origin=origin, target=target, power=power})
game.Debris:AddItem(script, 2)
T:Disconnect()
end)
end
local charging
function charge()
print'1'
script.Parent = Player.Backpack
charging=true
print'2'
while wait() do
print'3'
if power < MAX and not releasedClick then
print'4'
power = power + 2.5
else
print'5'
break
end
end
print'6'
if not released and not releasedClick then
print'7'
while wait() do
print'8'
if power - 5 <= 1 then
print'9'
power = 1
print'10'
released = true
print'11'
release()
print'12'
break
else
power = power - 10
end
end
end
end
Tool.Equipped:Connect(function()
releasedClick=nil
if not releasedClick and not released then
equipped=true
end
Mouse.TargetFilter = workspace
GUI.Parent = Player.PlayerGui
end)
Tool.Unequipped:Connect(function()
equipped=nil
GUI.Parent = Tool
end)
Mouse.Button1Down:Connect(function()
if equipped then
if not charging then
charge()
else
if not released then
release()
HoldTrack:Stop()
equipped=nil
end
end
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if equipped then
Line:TweenPosition(UDim2.new(power/MAX,0,.5,0),"Out", "Quad",.1,true)
if Hum.MoveDirection == Vector3.new(0,0,0) then
if not PlayingHold then
PlayingHold=true
HoldTrack:Play()
end
else
PlayingHold=nil
HoldTrack:Stop()
end
end
end)
ServerScript:
local freefalling
heartbeat = game:GetService('RunService').Heartbeat
buffer = Vector3.new(0.0001,0,0)
local Tool = script.Parent.Parent.Parent
local Character = Tool.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local Firer = script.Parent:WaitForChild("Fired")
local functions = {
["Throw"] = function(origin, FB, power, target, gravity)
spawn(function()
local tool = FB.Parent
local freeFalling = false
if tool:IsA('Tool') then
FB.Name = 'Football'
FB.CanCollide=true
FB.Parent = workspace
FB:SetNetworkOwner(nil)
--tool:Destroy()
local aim = (target-origin).unit
local spawnPos = origin + (aim * 5)
local targetPos = spawnPos + aim
FB.Velocity = aim*power
FB.CFrame=CFrame.new(spawnPos,targetPos)*CFrame.Angles(math.pi/2,0,0)
local bodyv = Instance.new('BodyVelocity',FB)
bodyv.MaxForce = Vector3.new(1,1,1)*1e6
bodyv.P=1e6
bodyv.Velocity=aim*power
--gravity
local flyingSound = FB:WaitForChild("FlyingSound")
flyingSound:Stop()
flyingSound:Play()
freeFalling=true
local stop
stop = coroutine.create(function()
freeFalling=false
if FB:FindFirstChild('BodyVelocity') then FB.BodyVelocity:Destroy() end
flyingSound:Stop()
coroutine.yield(stop)
end)
FB.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
FB.FootballClient.Disabled=true
FB:Destroy()
local Football = game.ServerStorage.Football:Clone()
Football.Parent = Hit.Parent
Football:WaitForChild("Handle"):WaitForChild("FootballClient").Disabled=false
coroutine.yield(stop)
else
coroutine.resume(stop)
end
end)
local i=0
while freeFalling do
i=i+0.25 --spiral speed
bodyv.Velocity = bodyv.Velocity + Vector3.new(0, gravity * heartbeat:wait(), 0)
local p = FB.CFrame.p
FB.CFrame=CFrame.new(p,p+bodyv.velocity+buffer)*CFrame.Angles(math.pi/2,math.pi*i,0)
print'looping'
end
end
end)
end
}
Firer.OnServerEvent:Connect(function(FPlayer, Option, Properties)
if FPlayer ~= Player then return end
if Option == "Throw" then
if Player.Character and Player.Character:FindFirstChild("Football") then
local target = Properties.target
local origin = Properties.origin
local power = Properties.power
local Football = Player.Character.Football.Handle
spawn(function()
functions[Option](origin, Football, power, target, -28)
end)
end
end
end)
Example of issue:
- First time you use it this happens:
https://gyazo.com/b5abe43bab8a709ace97094794295f8b - After picking the ball up and clicking again, this happens:
https://gyazo.com/30abc2716cefdcbb8032c2c76ee27f5a
NOTES:
- If you click, then wait till the bar reaches full power it will work the way it should.
- I tried relocating the scripts out of the tool, and the same problem occurred.