hi, i want to get some tips on writing scripts, my code works, but it looks sloppy. There is one problem related to the fact that holding down a key can cause a crash if the player moves the mouse or moves, but this rarely happens.
i have my custom ProximityPrompt, if you can still call it that, it works, but as I said, there are some minor problems.
-- Module
-- fast navigation
local nav = {
replic = game:GetService("ReplicatedStorage"); player = game:GetService("Players").LocalPlayer; prompt = script.Prompt;
input = game:GetService("UserInputService"); mobile = game:GetService("UserInputService").TouchEnabled; runservice = game:GetService("RunService");
--..
}
local Proximity = {}
-- create proximity
function Proximity.new(...)
local a = {...}
--print(a)
local function action(...)
print("action")
end
local function proximity() -- create screengui
if not nav.player:WaitForChild("PlayerGui", 2):FindFirstChild("ProximityPrompts") then
local proximity = Instance.new("ScreenGui");
proximity.Name = "ProximityPrompts";
proximity.Parent = nav.player:WaitForChild("PlayerGui", 2);
proximity.SafeAreaCompatibility = Enum.SafeAreaCompatibility.FullscreenExtension;
proximity.ScreenInsets = Enum.ScreenInsets.DeviceSafeInsets;
proximity.IgnoreGuiInset = true;
end
end
local function prompt() -- create prompt
proximity()
local billprompt = nav.prompt:Clone()
billprompt.Adornee = a[2]
billprompt.Parent = nav.player:WaitForChild("PlayerGui", 2):FindFirstChild("ProximityPrompts")
billprompt.Frame.TextFrame.ActionText.Text = a[6]; billprompt.Frame.TextFrame.ObjectText.Text = a[5] billprompt.Frame.InputFrame.ButtonText.Text = a[3]
local textbutton = billprompt.TextButton; local promt = billprompt.Frame; local down = false; local timer = 0
local function color(...)
local a = {...}
if a[1] == true then
promt.UIScale.Scale = .8;
promt.InputFrame.BackgroundColor3 = Color3.fromRGB(55, 55, 59);
promt.TextFrame.BackgroundColor3 = Color3.fromRGB(55, 55, 59);
elseif a[1] == false then
promt.UIScale.Scale = 1;
promt.InputFrame.BackgroundColor3 = Color3.fromRGB(14, 14, 15);
promt.TextFrame.BackgroundColor3 = Color3.fromRGB(14, 14, 15);
end
end
local function press()
local right = billprompt.Frame.InputFrame.CircularProgressBar["Frame-1"].ImageLabel
local left = billprompt.Frame.InputFrame.CircularProgressBar["Frame-2"].ImageLabel
local coldown = false
local rotation = 0
--if coldown == true then return end
if a[4] and coldown == false then
if a[4] ~= 0 then
color(true)
down = true
local length = a[4] * 30; local CL = 0
local cir = task.spawn(function()
repeat wait()
CL += 1; billprompt.Frame.InputFrame.CircularProgressBar.Progress.Value = CL/length * 100
local percent = math.clamp(billprompt.Frame.InputFrame.CircularProgressBar.Progress.Value * 3.6, 0, 360)
if percent <= 180 then
right.UIGradient.Rotation = percent
left.UIGradient.Rotation = 180
else
right.UIGradient.Rotation = 180
left.UIGradient.Rotation = percent
end
until CL/length * 100 >= 100 or down == false
color(false)
end)
local tim = task.spawn(function()
repeat wait(.1)
timer += .1
if timer >= a[4] then
action()
end
until timer >= a[4] or down == false
color(false)
right.UIGradient.Rotation = 0; left.UIGradient.Rotation = 180
down = false; timer = 0
end)
elseif a[4] == 0 then
color(true); task.wait(.1); color(false)
down = false; timer = 0
action()
end
end
end
if not nav.mobile then
nav.input.InputBegan:Connect(function(put) -- keybind
if put.KeyCode == Enum.KeyCode[a[3]] then
press()
end
end)
nav.input.InputEnded:Connect(function() -- keybind
down = false; timer = 0
end)
textbutton.MouseButton1Down:Connect(function() -- pc
press()
end)
textbutton.MouseButton1Up:Connect(function() -- pc
down = false; timer = 0
end)
elseif nav.mobile then
textbutton.TouchTap:Connect(function() -- mobile
press(); down = false; timer = 0
end)
textbutton.TouchLongPress:Connect(function() -- mobile
press()
end)
end
end
prompt()
end
return Proximity
-- LocalScript
-- fast navigation
local nav = {
replic = game:GetService("ReplicatedStorage"); proxy = require("@game/ReplicatedStorage/Modules/Proximity");
player = game:GetService("Players").LocalPlayer;
--..
}
-- managing Proximity being created
nav.proxy.new(
nav.player, --..player
workspace:WaitForChild("Part", 4), --..part
"E", --..keybind
0, --..cooldown press (0 = not cooldown for base click)
"Click", --..type
"John Doe" --..text
)
nav.proxy.new(
nav.player,
workspace:WaitForChild("Part-1", 4),
"R",
3,
"Hold",
"Roblox"
)
