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

"Userdata value" for Tool.Equipped?

Asked by 4 years ago

Hi, I'm trying to do simple stuff with guns. Here, when the player equips the gun, all the stuff happens. However, studio says: Pistol.LocalScript:12: attempt to call field 'Equipped' (a userdata value)

Here's the LocalScript:

--defining things
local MaxAmmo = script.Parent.ClipSize.Value
local Ammo = MaxAmmo
local Reloading = false
local reloadTime = script.Parent.ReloadTime
local gun = script.Parent

--Cursor
gun.Equipped(function(Mouse)
    Mouse.Icon = "rbxasset://textures://GunCursor.png"
    script.Parent.Activated:Connect(function()
        if Ammo>0 then  
            Ammo=Ammo-1
        elseif Reloading == false then
            Reloading = true
            Mouse.Icon = "rbxasset://textures://GunWaitCursor.png"
            wait(reloadTime.Value)
            Reloading = false
            Mouse.Icon = "rbxasset://textures://GunCursor.png"
        end
        print(Ammo)
    end)
end)

Any help is appreciated!

1 answer

Log in to vote
0
Answered by 4 years ago

Namaste it is i, dual

The reason it is erroring is because you are indexing gun.Equipped, something that is supposedly a value of type userdata. I believe what you want is the event .Equipped, and to connect to it. You should also disconnect the event when you are finished to it*

(https://developer.roblox.com/en-us/api-reference/event/Tool/Equipped)

Consider the following:

Change line 9 from

gun.Equipped(function(Mouse)

to

gun.Equipped:Connect(function(Mouse)

*You can read more on events and disconnecting events in ROBLOX here: https://developer.roblox.com/en-us/articles/events

That is all

Ad

Answer this question