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

Unable to cast Vector3 to token error. What's the cause of this?

I'm trying to send a Vector3 to a function to use for in a force value. But, when I call the function with a Vector3 parameter I get the error: Unable to cast Vector3 to token


local UIS = game:GetService('UserInputService')

local myCharacter = script.Parent
local myBody = myCharacter:WaitForChild('HumanoidRootPart')
local myVForce = Instance.new('VectorForce',myBody) -- Create a vector force obj in chest piece
myVForce.Attachment0 = myBody.RootRigAttachment
myVForce.Force = Vector3.new(0,0,0)

local dodgeKeys = {
	A = Vector3.new(-1500,0,0),
	D = Vector3.new(1500,0,0),
	S = Vector3.new(0,0,-1500),
	W = Vector3.new(0,0,1500)
}

-- Define Functions

local function getMass()
	local M = 0
	for i,Part in pairs(myCharacter:GetDescendants()) do
		if (Part:IsA('BasePart') and not Part.Massless) then
			M = M + Part.Mass
		end
	end
	return M
end

local function playerDodge(DodgeA)
	print('Dodge Called')
	local M = getMass()
	local F = M*DodgeA -- F=ma
	myVForce.Force = F
	wait(0.2)
	myVForce.Force = Vector3.new(0,0,0)
end

-- Interaction

UIS.InputBegan:Connect(function(key,i)
	print("Key Pressed")
	if (key.KeyCode == Enum.KeyCode.F) then
		local dodgeForce = Vector3.new(0,0,0)
		for Key,Vec in pairs(dodgeKeys) do
			if UIS:IsKeyDown(dodgeKeys[Key]) then
				dodgeForce = dodgeForce + dodgeKeys[Key]
			end
		end
		playerDodge(dodgeForce)
	end
end)

The error starts when I press the F and any of the WASD keys. Error msg says line 47. I'm really not sure what's going on here :\ Any help appreciated. How can I send the dodgeForce Vector3 to the playerDodge function?

1 answer

It appears that on line 44 you are trying to check using IsKeyDown if a Vector3 value is being pressed. IsKeyDown requires an Enum.KeyCode input rather than a Vector3 value.

1 comment

0

omg thanks :) Can't believe I didn't see that

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