Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make my interact system more efficient?

Asked by 3 years ago
Edited 3 years ago

I am making an interacting system, and I created the basic Interact system, how do I make it more efficient?

-- Local Script
local main = require(game.ReplicatedStorage.Resources.Modules.Shared.MainFramework)
main:setupScript() -- this is just to make the Services table work for my game.
local userInputService = game:GetService("UserInputService")
local runService = Services.RunService
local players = Services.Players
local localPlayer = players.LocalPlayer
local KEYBIND_FOR_INTERACTION = Enum.KeyCode.E
local debounceforTween = false
local deb = false
local function tweeninandout()

end
local remoteEvent = Services.ReplicatedStorage.Resources.Events.OnInputDetected
local function onInputBegan(...)
    for _, interactive in ipairs(workspace.Interactives:GetChildren()) do
        if interactive:FindFirstChild("IsInteractive") then
            local Character = localPlayer.Character
            if Character then
                local humanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
                if humanoidRootPart then
                    if Character:FindFirstChildWhichIsA("Humanoid").Health <= 0 then
                        break
                    else
                        local magnitude = (interactive.Position - humanoidRootPart.Position).magnitude
                        if magnitude <= interactive.IsInteractive.Range.Value then
                            local b = tweeninandOut()
                            if b then
                                return
                            end
                            remoteEvent:FireServer(interactive)
                        end
                    end
                end
            end
        end
    end
end
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == KEYBIND_FOR_INTERACTION then
            onInputBegan()
        end
    end
end)
local interactives = workspace.Interactives:GetChildren()
script.Parent.InteractiveFrame.KeyLabel.MouseButton1Click:Connect(onInputBegan)
runService.RenderStepped:Connect(function(rs)
    if deb then return end
    deb = true
    script.Parent.InteractiveFrame.Visible = false
    script.Parent.Overlay.Visible = false
    for _, interactive in pairs(interactives) do
        local character = localPlayer.Character
        if character then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            if humanoidRootPart then
                local magnitude = (interactive.Position - humanoidRootPart.Position).magnitude
                if magnitude <= interactive.IsInteractive.Range.Value then
                    script.Parent.InteractiveFrame.Visible = true
                    script.Parent.Overlay.Visible = true
                    script.Parent.InteractiveFrame.InteractText.Text = interactive.IsInteractive.TextToShow.Value
                    local Vector3toUDim2 = workspace.CurrentCamera:WorldToScreenPoint(interactive.Position)
                    script.Parent.InteractiveFrame.Position = UDim2.new(0, Vector3toUDim2.X, 0, Vector3toUDim2.Y);
                    script.Parent.Overlay.Position = UDim2.new(0, Vector3toUDim2.X, 0, Vector3toUDim2.Y);
                end
            end
        end
    end
    deb = false
end)

Answer this question