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

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

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

✓ Accepted answer

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!

2 comments

0

Ok thank you, Im working on a game function and intermission usally didn't worked!

0

stop like begging fr

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