Ad
Log in to vote
1

Function runs 1 more time, every time it's called. Any idea?

Asked by 7 days ago

Hello! This is my first question so point out anything I'm doing wrong.

I am attempting to make a game similar to Tower Defense Simulator. This is what I have for the tower placement script

LocalScript (in StarterGui)

-- Highlight Script --

-- Variables
local player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Remotes:WaitForChild('PlaceTower')
local button = script.Parent
local Highlight = game.Workspace.Highlight
local UIS = game:GetService('UserInputService')
local using = nil
local debounce = true

Highlight.Parent = nil

-- Setting and Configuring the Mouse
local mouse = player:GetMouse()
mouse.TargetFilter = Highlight

-- Place Function
local function place ()
    local clicked
    Highlight.Parent = game.Workspace
    UIS.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseButton1 and using then
            print('called')
            clicked = true
            -- Getting and Sending CFrame
            local HumanoidCFrame = Highlight:FindFirstChild('HumanoidRootPart').CFrame
            event:FireServer(HumanoidCFrame, 'Tower1')
            -- End
            Highlight.Parent = nil
        end
    end)

    while not clicked do
        wait()
        Highlight.HumanoidRootPart.CFrame = CFrame.new(mouse.Hit.p.X, mouse.Hit.p.Y + 1.315, mouse.Hit.p.Z)
    end

end

-- Button Detection
button.MouseButton1Up:Connect(function()
    using = true
    place()
    using = false

end)

Normal Script (in server script service)

-- Event Handlling Script --

-- ReplicatedStorage and Remotes
local ReplicatedStorage = game.ReplicatedStorage
local placeTower = ReplicatedStorage.Remotes:WaitForChild('PlaceTower')

-- Functions
local function placeTowerFunction(plr, cframe, tower)
    print('Placed tower')
    local Tower = ReplicatedStorage:WaitForChild(tower)
    local TowerClone = Tower:Clone()
    TowerClone.Parent = game.Workspace
    TowerClone.HumanoidRootPart.CFrame = cframe
end

-- Activate
placeTower.OnServerEvent:Connect(placeTowerFunction)

Basically, I would place one tower, and one tower would place. After that, I would place a second tower and two towers would place in the same spot, then 3, 4, and so on.

I would really appreciate any help, Thanks!

1 answer

Log in to vote
1
Answered by 7 days ago

I see the issue inside your "place" function. When the player clicks, it calls the "place" function which is fine but each time the place function is called, it connects a function to the "InputBegan" event. This causes an unwanted memory leak because you never disconnected the connection when the "place" function has finished running.

Whenever an event is connected, the connection never gets removed automatically unless you call the "Destroy" function on an instance. In the use you have, each time the "place" function gets ran, it connects a function to the "InputBegan" event and every time the player clicks, it calls that function that amount of times its been connected.

Here's a solution which disconnects the event connection after the "place" function has finished most of its work:

local function place ()
    local clicked
    local connection -- New variable to hold the "InputBegan" Connection
    Highlight.Parent = game.Workspace
    connection = UIS.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseButton1 and using then
            print('called')
            clicked = true
            -- Getting and Sending CFrame
            local HumanoidCFrame = Highlight:FindFirstChild('HumanoidRootPart').CFrame
            event:FireServer(HumanoidCFrame, 'Tower1')
            -- End
            Highlight.Parent = nil
        end
    end)

    while not clicked do
        wait()
        Highlight.HumanoidRootPart.CFrame = CFrame.new(mouse.Hit.p.X, mouse.Hit.p.Y + 1.315, mouse.Hit.p.Z)
    end

    connection:Disconnect() -- Run this function wherever you think is right
end
0
Oh my lord thank you so much!! MarTieMak 39 — 7d
0
np Retr0Thief 104 — 6d
Ad

Answer this question