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

Help with detecting Character?

Asked by
NykoVania 231 Moderation Voter
1 year ago

I get 2 errors in output:

attempt to index nil with Parent - Line 6

and

Argument 1 missing or nil - Line 12

I have no idea how to troubleshoot these errors and I need assistance with them if possible.

Code:

local Player = script.Parent.Parent.Parent
local Char = Player.Character
Char.Hitboxes.Part.Touched:Connect(function(inst)
    if Char.Hitboxes.Part.Active.Value == true then
        local player
        if game:GetService("Players"):GetPlayerFromCharacter(inst.Parent) or inst.Parent:FindFirstChildOfClass("Humanoid").Parent.Name then
            player = game:GetService("Players"):GetPlayerFromCharacter(inst.Parent) or inst.Parent.Name
            else print("no")
        end
        --------
        local Character
        if player.Character or game.Workspace[player.Name] then
            Character = player.Character or game.Workspace[player.Name]
        else print("no")
        end
        print(Character)
        print(Player.Name .. Player.Parent.Name)
    end
end)

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

don't really understand what you're trying to accomplish with your script, but if theres script.Parent.Parent.Parent then it sets up a new connection every time someone joins.

also, you should pick one and do GetPlayerFromCharacter or Humanoid.Parent.Name.

Detecting the character here is unnecessary. If you get the player with any of the two above, you already know the character will be inst.Parent

part.Touched:Connect(function(object)
    local character = object.Parent
    local player = Players:GetPlayerFromCharacter(character)
    if player and character then
        print("both player and character detected")
    end
end)
0
I'm aware it sets a new connection, it's a function for each player. Thank you for your help! NykoVania 231 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Change

if Char.Hitboxes.Part.Active.Value == true then

to

if Char.Hitboxes.Part.Active.Value == true and inst ~= nil then

Answer this question