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

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

Asked by 3 years 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)

01-- Highlight Script --
02 
03-- Variables
04local player = game.Players.LocalPlayer
05local event = game.ReplicatedStorage.Remotes:WaitForChild('PlaceTower')
06local button = script.Parent
07local Highlight = game.Workspace.Highlight
08local UIS = game:GetService('UserInputService')
09local using = nil
10local debounce = true
11 
12Highlight.Parent = nil
13 
14-- Setting and Configuring the Mouse
15local mouse = player:GetMouse()
View all 47 lines...

Normal Script (in server script service)

01-- Event Handlling Script --
02 
03-- ReplicatedStorage and Remotes
04local ReplicatedStorage = game.ReplicatedStorage
05local placeTower = ReplicatedStorage.Remotes:WaitForChild('PlaceTower')
06 
07-- Functions
08local function placeTowerFunction(plr, cframe, tower)
09    print('Placed tower')
10    local Tower = ReplicatedStorage:WaitForChild(tower)
11    local TowerClone = Tower:Clone()
12    TowerClone.Parent = game.Workspace
13    TowerClone.HumanoidRootPart.CFrame = cframe
14end
15 
16-- Activate
17placeTower.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 3 years 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:

01local function place ()
02    local clicked
03    local connection -- New variable to hold the "InputBegan" Connection
04    Highlight.Parent = game.Workspace
05    connection = UIS.InputBegan:Connect(function(input)
06        if input.UserInputType == Enum.UserInputType.MouseButton1 and using then
07            print('called')
08            clicked = true
09            -- Getting and Sending CFrame
10            local HumanoidCFrame = Highlight:FindFirstChild('HumanoidRootPart').CFrame
11            event:FireServer(HumanoidCFrame, 'Tower1')
12            -- End
13            Highlight.Parent = nil
14        end
15    end)
View all 23 lines...
0
Oh my lord thank you so much!! MarTieMak 56 — 3y
0
np Retr0Thief 104 — 3y
Ad

Answer this question