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

Unable to cast value to object?

I removed the backpack with

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

Because of this, I'm unable to select weapons so I decided to make my own script for this purpose, it's able to print the gun and the player when I click 1 but it gives me an error "Unable to cast value to object"

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
local plr = script.Parent.Parent
local plrname = plr.Name


Mouse.KeyDown:connect(function(key)
	if key == "1" then
		local gun = script.Parent.Menu.Frame.Spawn.ForceEQ.Value.Value
		print(gun)
		local player = game.Workspace:FindFirstChild(plrname)
		print(player)
		player.Humanoid:EquipTool(gun) -- ERROR IS HERE
	end
end)


1 comment

0

Key down is deprecated use userinputservice and 'gun' is not an object its a value. You cant equip a value :)

2 answers

✓ Accepted answer

Before I get to your question, I will tell you to not use KeyDown. It is deprecated. Instead, use UserInputService.

Example of using UserInputService:

game:GetService("UserInputService"):InputBegan:Connect(function(key)
	if key == Enum.KeyCode.Q then
		print("Q pressed!")
	end
end)

Now, as to your question. The variable gun is not a tool. It is a value. As what LordOfWatermelons said, you cannot equip a value... lol...

To fix this, make sure the tool you are trying to equip is a tool.

nvm found my own answer here it is if anyone wants it

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
local plr = script.Parent.Parent
local plrname = plr.Name




Mouse.KeyDown:connect(function(key)
	if key == "1" then
		local gun = script.Parent.Menu.Frame.Spawn.ForceEQ.Value.Value
		print(gun)
		local player = game.Workspace:FindFirstChild(plrname)
		local gun2 = plr.Backpack:FindFirstChild(gun)
		print(player)
		player.Humanoid:EquipTool(gun2) -- ERROR IS HERE
	end
end)


1 comment

0

wow lol

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