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

Key still not working/broken?

As you can see my problem is that once the player presses the shift key he should follow the mouse and once he presses it again he should stop following the mouse and move normally after this, but it seems that if i press any key but shift it deactivates the player following mouse script again after i activate it with shift again. You can try this script yourself by putting

local mode = 0 -- 0 = default, 1 = battle.
local gyro = Instance.new('BodyGyro')
gyro.maxTorque = Vector3.new(1400000,1400000,1400000) 
wait()
local cFrame = CFrame.new(0, 0, 0)
local torso = player.Character.Torso

mouse.KeyDown:connect(function(key)
	if mode == 0 then
gyro.Parent = game.Lighting
	if string.byte(key) == 48 then
	mode = 1
	end	
end

	if mode == 1 then
gyro.Parent = torso
	if string.byte(key) == 48 then
	mode = 0
	end
end

game:GetService("RunService").RenderStepped:connect(function()
local position = mouse.Hit.p
local setPosition = Vector3.new(position.X, torso.Position.Y, position.Z)
local direction = (setPosition - torso.Position).unit
cFrame = CFrame.new(torso.Position, torso.Position + direction)
gyro.cframe = cFrame
end)

3 comments

1

If your problem is fixed, please do not delete the question! You can post additional questions, so there is no value in deleting your question!

0

I'm very sorry i will not do this again, i'm new here so i got a lot to learn.

0

I still need help though as i have been trying to fix this for hours. :/

2 answers

✓ Accepted answer

You should probably use elseif mode == 1 then. But if you want to hold the key and release it to stop you do this.

mouse.KeyUp:connect(function(key)

	if string.byte(key) == 48 then
		mode = 1
		gyro.Parent = game.Lighting
	end
end)

mouse.KeyDown:connect(function(key)

	if string.byte(key) == 48 then
		mode = 0
		gyro.Parent = torso
	end	
end)

1 comment

0

Thanks i guess i'll just go with holding the key instead of tapping it :)

Im pretty sure your problem is where you check "if" mode == () then: use elseif in order to run both of them if the first one is incorrect.

local mode = 0 -- 0 = default, 1 = battle.
local gyro = Instance.new('BodyGyro')
gyro.maxTorque = Vector3.new(1400000,1400000,1400000) 
wait()
local cFrame = CFrame.new(0, 0, 0)
local torso = player.Character.Torso

mouse.KeyDown:connect(function(key)
    if mode == 0 then
gyro.Parent = game.Lighting
    if string.byte(key) == 48 then
    mode = 1
    end 
end

   elseif mode == 1 then
gyro.Parent = torso
    if string.byte(key) == 48 then
    mode = 0
    end
end

1 comment

0

it still does not work :/ oh well.

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