What can I do to lower script activity?

I noticed that my memory would slowly be going up in some instances and then fall back down and repeat the process again later at irregular intervals. I looked in the script performance tab and noticed that a local script that managed a weapon had frequent activity after each use. It would increase and decrease randomly in activity, but would never fall back down to 0%. What can I do to fix this?

local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.

local Player = game.Players.LocalPlayer

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")

Tool.Equipped:Connect(function()
	UserInputService.MouseIcon = MOUSE_ICON
end)

Tool.Unequipped:Connect(function()
	UserInputService.MouseIcon = ""
end)

Tool.Activated:Connect(function()
	local cooldownTime = activateBombRemote:FireServer(Player)
	
	UserInputService.MouseIcon = RELOADING_ICON
	
	repeat task.wait() until cooldownTime == false

	UserInputService.MouseIcon = MOUSE_ICON
end)
1 Like

Nothing in here should be majorly affecting memory aside from the repeat task.wait() until if the cooldownTime is never set to false. Have you debugged to make sure it does get set to false?

You stated the memory never falls back down after rising, this could be because the repeat loop runs indefinitely once ran and never stopped, creating a constant cycle and potentially affecting memory.

Now that I think about it, the player can keep activating the tool several times and the remote event will constantly fire, but since the cooldown will be true on the server, it won’t return anything.

1 Like

What I said above could be true in an instance, but in this one it’s because I am using a remote event and not a remote function, so I’ll look into what I can do.

1 Like

local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Cursor Icon.

local Player = game.Players.LocalPlayer
local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")

-- Reference to the player's mouse
local playerMouse = Player:GetMouse()

Tool.Equipped:Connect(function()
    playerMouse.Icon = MOUSE_ICON
end)

Tool.Unequipped:Connect(function()
    playerMouse.Icon = ""
end)

Tool.Activated:Connect(function()
    -- Fire the server to get the cooldown time
    local cooldownTime = activateBombRemote:FireServer()
    
    -- Set the cursor to the reloading icon
    playerMouse.Icon = RELOADING_ICON
    
    -- Wait until the cooldown is done
    wait(cooldownTime)
    
    -- Reset the mouse icon back to the gun cursor
    playerMouse.Icon = MOUSE_ICON
end)

1 Like

local activateBombRemote = game.ReplicatedStorage:WaitForChild("ActivateBomb")

activateBombRemote.OnServerEvent:Connect(function(player)
    local cooldownTime = 5 -- Set the cooldown time to 5 seconds or whatever you want
    -- You could also add logic here for the actual bomb activation, etc.
    
    -- Return the cooldown time to the client
    activateBombRemote:FireClient(player, cooldownTime)
end)

1 Like