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

Key still not working/broken?

Asked by
Hero_ic 502 Moderation Voter
9 years ago

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)
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! Unclear 1776 — 9y
0
I'm very sorry i will not do this again, i'm new here so i got a lot to learn. Hero_ic 502 — 9y
0
I still need help though as i have been trying to fix this for hours. :/ Hero_ic 502 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

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)
0
Thanks i guess i'll just go with holding the key instead of tapping it :) Hero_ic 502 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

0
it still does not work :/ oh well. Hero_ic 502 — 9y

Answer this question