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

Why is my local script not able to find a model that is there?

Asked by 6 years ago

I made this script so you can attack with this weird arm on your torso (Don't ask why)... The error it give me "Players.kittonlover101.Backpack.Pick2.LocalScript:5: attempt to index local 'character' (a nil value)". The error is on line 5. Any help would be appreciated. Also the 3rd Arm is a model with its own humanoid.

local CanAttack = true
local player = script.Parent.Parent
local playername = player.Name
local character = game.Workspace:FindFirstChild(playername)
local arm = character:WaitForChild("3rd Arm")


script.Parent.Activated:connect(function()

wait(0.1)
local attack = arm.Humanoid:LoadAnimation (script.Attack)

attack:Play()

wait(1)

attack:Stop()

end)

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

You can use the Character property of Player to get their character. No need to search workspace for it.

local CanAttack = true
local plr = game:GetService("RunService"):IsClient() and game:GetService("Players").LocalPlayer or script:FindFirstAncestorWhichIsA("Player") -- you didnt specify if its a localscript or not /shrug

local char = plr.Character or plr.CharacterAdded:Wait() -- wait for character if it isnt created
local arm = char:WaitForChild("3rd Arm")

script.Parent.Activated:Connect(function()
    wait(0.1)
    local attack = char.Humanoid:LoadAnimation(script.Attack)
    attack:Play()
    wait(1)
    attack:Stop()
end)

(Sorry for any typos, I'm on phone and I rewrote your script instead of copy pasting it)

Ad

Answer this question