My game is lagging a bit because I did this function that returns a variable from a local script but i use it in a loop which isn’t very optimized, is there something I can do so the game doesn’t lag anymore? Also I will try to put my functions in a module script but I’m not sure if it will solve the issue.
The function in server sided script:
local function getButtonState(player)
remoteEvent:FireClient(player, "requestButtonState")
local player, buttonState = remoteEvent.OnServerEvent:Wait()
return buttonState
end
The loop:
local function punch(player, side)
local timer = tick()
local humanoid = player.Character:FindFirstChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local punchAnim = Instance.new("Animation")
punchAnim.AnimationId = chooseAnimation(punchAnimations, player, side)
local punchTrack = animator:LoadAnimation(punchAnim)
punchTrack.Looped = false
punchTrack:Play()
sound_swingBack:Play()
punchTrack:GetMarkerReachedSignal("Guarding"):Wait()
punchTrack:AdjustSpeed(0)
while getButtonState(player) == true do --Here!!
task.wait()
end
punchTrack:AdjustSpeed(1)
sound_swingForward:Play()
hitbox(player, (tick() - timer))
punchTrack.Ended:Wait()
remoteEvent:FireClient(player,"animEnded")
end
And the local script event:
remoteEvent.OnClientEvent:Connect(function(action)
if action == "altAnim" then
altAnim = not altAnim
elseif action == "animEnded" then
canAct = true
elseif action == "requestButtonState" then
remoteEvent:FireServer(keyPressed) --Here!!
end
end)
Thanks for any help!! :))