I have a script that detects when the player is freefalling, then when the player presses space, it should clone a single billboard gui. issue is, it clones multiple instead of only one ( https://gyazo.com/bf4de8a61fa8b36e0b7135516482d22d )
is this because of freefall? is there a resolve to this?
UIS = game:GetService("UserInputService")
Plr = game.Players.LocalPlayer
Plr.CharacterAdded:Connect(function()
local Char = Plr.Character
local Hum = Char:WaitForChild("Humanoid")
local HumRP = Char:WaitForChild("HumanoidRootPart")
local HS = Hum:GetState()
local Targs = workspace.JumpTargets:GetChildren()
Hum.StateChanged:Connect(function()
HS = Hum:GetState()
if HS == Enum.HumanoidStateType.Freefall then
local partsTable = Targs
local closest = {nil,50}
for i,v in pairs(partsTable) do
local distance = (HumRP.Position - v.Position).magnitude
if distance < closest[2] then
closest = {v, distance}
print(closest)
end
end
local CD1 = false
if closest[2] < 50 then
print("Within range")
if CD1 == false then
CD1 = true
wait(0.5)
UIS.InputBegan:Connect(function(Key)
Key = Key.KeyCode
if Key == Enum.KeyCode.Space then
if HS == Enum.HumanoidStateType.Freefall then
local RealTarg = game.ReplicatedStorage["PH1--Target"].BillboardGui:Clone()
RealTarg.Parent = closest[1]
script["Lock On"]:Play()
wait(3)
RealTarg:Destroy()
end
end
end)
else print("No nearby targets found")
end
end
end
end)
end)
i tried a debounce but it seems that freefall is too “fast” for it i think i know actually, i’ll try apply it somewhere else – didn’t work
Try making use of ContextActionService, would be perfect for this. Also .StateChanged contains both the old state and the new state, so you don’t need to use Hum:GetState().
local ContextActionService = game:GetService("ContextActionService")
local falling = false
local function func()
-- stuff you want to happen here
end
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Freefall then
falling = true
ContextActionService:BindAction("actionName", func, false, Enum.PlayerActions.CharacterJump)
elseif falling then
falling = false
ContextActionService:UnbindAction("actionName")
end
end)
local UIS = game:GetService("UserInputService")
local Plr = game.Players.LocalPlayer
local debounce = false
Plr.CharacterAdded:Connect(function()
local Char = Plr.Character
local Hum = Char:WaitForChild("Humanoid")
local HumRP = Char:WaitForChild("HumanoidRootPart")
local HS = Hum:GetState()
local Targs = workspace.JumpTargets:GetChildren()
Hum.StateChanged:Connect(function()
HS = Hum:GetState()
if HS == Enum.HumanoidStateType.Freefall then
local partsTable = Targs
local closest = {nil,50}
for i,v in pairs(partsTable) do
local distance = (HumRP.Position - v.Position).magnitude
if distance < closest[2] then
closest = {v, distance}
end
end
if closest[2] < 50 then
UIS.InputBegan:Connect(function(Key)
Key = Key.KeyCode
if Key == Enum.KeyCode.Space then
if HS == Enum.HumanoidStateType.Freefall then
if debounce then
return
end
debounce = true
local RealTarg = game.ReplicatedStorage["PH1--Target"].BillboardGui:Clone()
RealTarg.Parent = closest[1]
script["Lock On"]:Play()
task.wait(3)
RealTarg:Destroy()
debounce = false
end
end
end)
else
task.wait(1)
end
end
end)
end)
A big issue here (from a debugging standpoint) is that the code isn’t indented properly. Thankfully that’s an easy fix. Right click in the script editor and use Format:
You have it set up in a way that you run UIS.InputBegan:Connect(function(Key) every time state changes to freefall. So every time they enter freefall, you connect the InputBegan function again. So every time it happens, it adds to the number of Guis. I’ve fixed that issue I believe. I would have rather done it a different way so as to remove UIS.InputBegan from the function altogether, but I figure this is better since you understand your code and I don’t.
UIS = game:GetService("UserInputService")
Plr = game.Players.LocalPlayer
Plr.CharacterAdded:Connect(function()
local Char = Plr.Character
local Hum = Char:WaitForChild("Humanoid")
local HumRP = Char:WaitForChild("HumanoidRootPart")
local HS = Hum:GetState()
local Targs = workspace.JumpTargets:GetChildren()
local Connection
Hum.StateChanged:Connect(function()
HS = Hum:GetState()
if HS == Enum.HumanoidStateType.Freefall then
local partsTable = Targs
local closest = {nil,50}
for i,v in pairs(partsTable) do
local distance = (HumRP.Position - v.Position).magnitude
if distance < closest[2] then
closest = {v, distance}
print(closest)
end
end
local CD1 = false
if closest[2] < 50 then
print("Within range")
if CD1 == false then
CD1 = true
wait(0.5)
if Connection then Connection:Disconnect() end
Connection = UIS.InputBegan:Connect(function(Key)
Key = Key.KeyCode
if Key == Enum.KeyCode.Space then
if HS == Enum.HumanoidStateType.Freefall then
local RealTarg = game.ReplicatedStorage["PH1--Target"].BillboardGui:Clone()
RealTarg.Parent = closest[1]
script["Lock On"]:Play()
wait(3)
RealTarg:Destroy()
end
end
end)
else
print("No nearby targets found")
Connection:Disconnect()
end
else
Connection:Disconnect()
end
else
Connection:Disconnect()
end
end)
end)