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

moving a ball with a keypress not working error?

Asked by 5 years ago
Edited 5 years ago

I want to have a ball move when I hit W... I succesfully put in place the movement and I have the BodyVelocity in the script. I set the MaxForce and Velocity all to zero so when I'm not hitting w the ball will roll will gravity. The UserInputService is working but I get the error message "X cannot be assigned to"... thats it, it doesn't say what the coordinate x cannot be assigned to... Eventually I will have a system in place so the ball slows down with gravity instead of stopping immidiatly but here is what I have now. This is all in a local script.

local UIs = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'W'

UIs.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] then
    script.Parent.BodyVelocity.Velocity.X = 50
    script.Parent.BodyVelocity.MaxForce.X = 100
    end
end)

1 answer

Log in to vote
2
Answered by 5 years ago

You cannot set individual components of Vector3 values. Here's how you fix that:

local UIs = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'W'

UIs.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] then
    script.Parent.BodyVelocity.Velocity = Vector3.new(50,script.Parent.BodyVelocity.Velocity.Y,script.Parent.BodyVelocity.Velocity.Z)
    script.Parent.BodyVelocity.MaxForce = Vector3.new(100,script.Parent.BodyVelocity.MaxForce.Y,script.Parent.BodyVelocity.MaxForce.Z)
    end
end)
Ad

Answer this question