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

ANSWERED: Why am I getting the error "Unable to cast token to token" and what does this mean?

I am adapting the following Roblox script to create a sprint code

local UserInputService = game:GetService("UserInputService")
 
-- Shift keys
local shiftKeyL = Enum.KeyCode.LeftShift
local shiftKeyR = Enum.KeyCode.RightShift
 
-- Return whether left or right shift keys are down
local function IsShiftKeyDown()
	return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR)
end
 
-- Handle user input began differently depending on whether a shift key is pressed
local function Input(input, gameProcessedEvent)
	if not IsShiftKeyDown() then
		-- Normal input
	else
		-- Shift input
	end
end
 
UserInputService.InputBegan:Connect(Input)

Above is the original code.

Below is the code I have at this moment.

local UserInputService = game:GetService("UserInputService")
 
-- Shift keys
local shiftKeyL = Enum.KeyCode.LeftShift
local shiftKeyR = Enum.KeyCode.RightShift
 
-- Return whether left or right shift keys are down
local function IsShiftKeyDown()
	return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR) or UserInputService:IsGamepadButtonDown(Enum.KeyCode.ButtonL1)
end
 
-- Handle user input began differently depending on whether a shift key is pressed
local function Input(input, gameProcessedEvent)
	if not IsShiftKeyDown() then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 -- Normal input
	else
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32 -- Shift input
	end
end
 
UserInputService.InputBegan:Connect(Input)

And I am getting the following Output:

  18:22:53.803 - Unable to cast token to token
18:22:53.805 - Stack Begin
18:22:53.805 - Script 'Workspace.IncredibleTeamAidan.Run New', Line 9 - function IsShiftKeyDown
18:22:53.806 - Script 'Workspace.IncredibleTeamAidan.Run New', Line 14 - function Input
18:22:53.807 - Stack End

It appears that the issue is with detecting the buttons pressed in lines 8 to 10

local function IsShiftKeyDown()
	     return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR) or UserInputService:IsGamepadButtonDown(Enum.KeyCode.ButtonL1)
end

however I believe it could be with line 21

 UserInputService.InputBegan:Connect(Input)

If anybody knows how I may be able to adjust this to work, that would be much appreciated

Other Information: Code is within a local script, stored in Game.StarterPlayer.StarterCharacterScripts

1 comment

0

Please don't take code from the ROBLOX wiki.

1 answer

✓ Accepted answer

The error message "Unable to cast token to token" means that the code is trying to use the wrong kind of Enum somewhere (Enum variant values are called tokens in Roblox). Checking the API reference for IsGamepadButtonDown [1], we find out that the method takes two arguments instead of one. The first one is Enum.UserInputType, identifying the gamepad to be queried. The second one is Enum.KeyCode, specifying which button on that gamepad should be checked. So, what you probably meant to write is:

    return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR) or UserInputService:IsGamepadButtonDown(Enum.UserInputType.Gamepad1, Enum.KeyCode.ButtonL1)

[1]: Roblox DevHub: UserInputService:IsGamepadButtonDown

1 comment

0

Thank you! I never would have realised it was as simple as that.

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