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

How Do I fix this Workspace.TheGame:506: attempt to index local 'char' (a nil value)?

Asked by 4 years ago

I have tried many things theres the part it tells me....

for i,v in pairs(game.Players:GetChildren()) do local char = v.Character local head = char:FindFirstChild("Head") if head then for i,v2 in pairs(head:GetChildren()) do if v2.Name == "JumpAttactSound" then v2:Destroy()

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

From what I've examined, your program doesn't have any syntax issues, which brings me to a final conclusion. Your Script isn't local; the Server cannot gain access directly to any Client related information due to Filtering Enabled, therefore you'll need to take an approach that allows or gives that information without asking for it. One way of doing so is using the PlayerAdded event of the Players service. Adjusting to that with your Script should look like this:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player) -- This event passes the Player
    local Character = Player.Character or Player.CharacterAdded:Wait()
    for _,instance in ipairs(Character.Head:GetDescendants()) do
        if (instance.Name == "JumpAttackSound") then
            instance:Destroy()
        end
    end
end)

What's happening here?

All this change involves a lot more advanced approaches, so I'll explain them to you.

Firstly, when we declare the Character variable, we added an extra piece to the line to ensure that we won't get a nil value for the case that we asked for it before it was loaded.

Afterwards, we use an underscore within the for loop, which simply tells the program: we don't need i, same thing goes for v, or "instance". Then we use the GetDescendants metamethod, which simply gives us every possible instance within Head, just in case JumpAttackSound is a little further than we anticipated.

That's it! Remember, if this works, don't forget to accept and upvote!

0
Ok thank you, Im working on a game function and intermission usally didn't worked! KEVINRUANO 12 — 4y
0
stop like begging fr raid6n 2196 — 4y
Ad

Answer this question