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

Unable to cast value to object?

Asked by 6 years ago

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)


0
Key down is deprecated use userinputservice and 'gun' is not an object its a value. You cant equip a value :) LordOfWatermelons 27 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago

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)


0
wow lol Operation_Meme 890 — 6y

Answer this question