Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is does the Character Movement exponentially speed up in this situation?

I am working on a custom Control Script, containing a local script, a server script, and a remote event (referred to as the variable "r"), it is supposed to operate in a sense that when you press 'w', 'a', 's', or 'd' your character's torso CFrame will move forward/backward according to lookVector, and/or Rotate, here are the scripts so far: Local Script:

local r = game:GetService("ReplicatedStorage"):WaitForChild("Control");
local uis 									= game:GetService("UserInputService");
local cas 									= game:GetService("ContextActionService");
uis.MouseIconEnabled = false;
Pause 											= function() print("p"); end
cas:BindAction("Pause", Pause, true, Enum.KeyCode.P);
uis.InputBegan:connect(function(input)
	while table.getn(uis:GetKeysPressed()) > 0x00 do
		for i = 0x01, table.getn(uis:GetKeysPressed()) do
			r:FireServer(uis:GetKeysPressed()[i].KeyCode);
		end
		wait();
	end
end);

Server Script:

script.Control.Parent = game:GetService("ReplicatedStorage");
local r = game:GetService("ReplicatedStorage"):WaitForChild("Control");
r.OnServerEvent:connect(function(plr, info)
	if info == Enum.KeyCode.W then
		plr.Character:FindFirstChild("Torso").CFrame = 
			plr.Character:FindFirstChild("Torso").CFrame + 
		  plr.Character:FindFirstChild("Torso").CFrame.lookVector;
	elseif info == Enum.KeyCode.S then
		plr.Character:FindFirstChild("Torso").CFrame = 
			plr.Character:FindFirstChild("Torso").CFrame - 
		  plr.Character:FindFirstChild("Torso").CFrame.lookVector;
	elseif info == Enum.KeyCode.A then
		plr.Character:FindFirstChild("Torso").CFrame = 
			plr.Character:FindFirstChild("Torso").CFrame * 
			CFrame.fromEulerAnglesXYZ(0, 0.15, 0);
	elseif info == Enum.KeyCode.D then
		plr.Character:FindFirstChild("Torso").CFrame = 
			plr.Character:FindFirstChild("Torso").CFrame * 
			CFrame.fromEulerAnglesXYZ(0, -0.15, 0);
	elseif info == Enum.KeyCode.P then
		
	end
end)

This works really well, however whenever I continuously press 'w', 'a', 's', and/or 'd', without stopping, the character begins moving extremely fast, which I do not want as the character will spiral out of control due to the speed. If anyone could let me know why they think this is happening, and how they think it could be fixed please let me know. Help would be much appreciated, thanks!

2 comments

0

Your spacing in the first code block is very odd, and why are those 2 numbers represented in hexadecimal? I know it doesn't have anything to do with your question, but you may want to re-consider those things.

0

the actual scritps spacing is normal, I just had trouble pasting in the local script as the first 3 lines were really long

1 answer

✓ Accepted answer

Based on your description, the problem is that InputBegan is probably firing periodically (same sort of thing happens when you type into a textbox and just hold down a single key for a few seconds), and so you have numerous while loops occurring. You just need to add debounce:

local r = game:GetService("ReplicatedStorage"):WaitForChild("Control");
local uis                                   = game:GetService("UserInputService");
local cas                                   = game:GetService("ContextActionService");
uis.MouseIconEnabled = false;
Pause                                           = function() print("p"); end
cas:BindAction("Pause", Pause, true, Enum.KeyCode.P);
db = false
uis.InputBegan:connect(function(input)
	if db then return end
	db = true
    while table.getn(uis:GetKeysPressed()) > 0x00 do
        for i = 0x01, table.getn(uis:GetKeysPressed()) do
            r:FireServer(uis:GetKeysPressed()[i].KeyCode);
        end
        wait();
    end
	db = false
end);

1 comment

0

thanks, in the long run I got the control script to function properly!

This discussion is preserved as it was. New answers, comments, and votes are closed.