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

Why can't the player move when this script is enabled?

Asked by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

I'm having a problem with this script, the player can't move somehow.

Help will be greatly appreciated

local water=workspace.Water
local rs=game:GetService("RunService")
local underwater=false
local player=game.Players.LocalPlayer
local char=player.Character or player.CharacterAdded:wait()
local cas=game:GetService("ContextActionService")
local keyW,keyA,keyS,keyD,keyshift,keyspace=false,false,false,false,false,false

function stuff()
    game.Lighting.Ambient=Color3.new(9/255, 137/255, 207/255)
    game.Lighting.OutdoorAmbient=Color3.new(9/255, 137/255, 207/255)
    if not game.SoundService.Sound:FindFirstChildOfClass('CompressorSoundEffect') then
        Instance.new('CompressorSoundEffect').Parent=game.SoundService.Sound
    end
end

function reverse()
    game.Lighting.Ambient=Color3.new()
    game.Lighting.OutdoorAmbient=Color3.new(127/255,127/255,127/255)
    if char.Torso:FindFirstChildOfClass('BodyVelocity') then
        char.Torso:FindFirstChildOfClass('BodyVelocity'):Destroy()
    end
    if game.SoundService.Sound:FindFirstChildOfClass('CompressorSoundEffect') then
        game.SoundService.Sound:FindFirstChildOfClass('CompressorSoundEffect'):Destroy()
    end
end


function mechanics()
    if not char.Torso:FindFirstChildOfClass("BodyVelocity") then
        local bv=Instance.new("BodyVelocity",char.Torso)
        bv.Name='BodyVelocity'
        bv.MaxForce=Vector3.new(999999999999999999999999999999999999999999,999999999999999999999999999999999999999999,999999999999999999999999999999999999999999)
        bv.Velocity=Vector3.new()
    end
end

rs.RenderStepped:Connect(function()
    water.CFrame=water.CFrame*CFrame.new(math.sin(tick())*1/17,0,0)
    if math.floor((char:GetPrimaryPartCFrame().p+Vector3.new(0,1.5,0)).Y)<=water.CFrame.p.Y and (char.Torso.Position.X-(char.Torso.Size.X/2)) >= (water.CFrame.p.X-(water.Size.X/2)) and (char.Torso.Position.X+(char.Torso.Size.X/2)) <= (water.CFrame.p.X+(water.Size.Z/2)) and (char.Torso.Position.Z-(char.Torso.Size.Z/2)) >= (water.CFrame.p.Z-(water.Size.Z/2)) and (char.Torso.Position.Z+(char.Torso.Size.Z/2)) <= (water.CFrame.p.Z+(water.Size.Z/2)) then
        print("Underwater")
        underwater=true
        mechanics()
        stuff()
    else
        print("Not underwater")
        underwater=false
        reverse()
    end
    if keyW then
        char.Torso.BodyVelocity.Velocity=Vector3.new(0,0,-7.5)
    end
    if keyA then
        char.Torso.BodyVelocity.Velocity=Vector3.new(-7.5,0,0)
    end
    if keyS then
        char.Torso.BodyVelocity.Velocity=Vector3.new(0,0,7.5)
    end
    if keyD then
        char.Torso.BodyVelocity.Velocity=Vector3.new(7.5,0,0)
    end
    if keyshift then
        char.Torso.BodyVelocity.Velocity=Vector3.new(0,-7.5,0)  
    end
    if keyspace then
        char.Torso.BodyVelocity.Velocity=Vector3.new(0,7.5,0)
    end
end)

cas:BindAction('pressw',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyW=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(0,0,-7)
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyW=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.W)

cas:BindAction('pressa',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyA=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(-7,0,0)
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyA=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.A)

cas:BindAction('presss',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyS=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(0,0,7)
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyS=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.S)

cas:BindAction('pressd',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyD=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(7,0,0)
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyD=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.D)

cas:BindAction('pressSHIFT',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyshift=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(0,-7,0) 
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyshift=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.LeftShift)

cas:BindAction('pressSPACE',function(a,s,k)
    if s==Enum.UserInputState.Begin then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyspace=true
            --char.Torso.BodyVelocity.Velocity=char.Torso.BodyVelocity.Velocity+Vector3.new(0,7,0)
        end
    elseif s==Enum.UserInputState.End then
        if char.Torso:FindFirstChildOfClass("BodyVelocity") then
            keyspace=false
            char.Torso:FindFirstChildOfClass("BodyVelocity").Velocity=Vector3.new()
        end
    end
end,false,Enum.KeyCode.Space)

Thanks, chexyalani

0
On line 7 it says all WASD is false, Making the players unable to move until all of the keys are set to true Mohawkid14 28 — 6y
0
That's not the answer, but I found out that if I comment out the key binding part of the script, the player can move, but I don't know why. Rare_tendo 3000 — 6y

Answer this question