Ad
Log in to vote
0

Help with detecting Character?

Asked by
NykoVania 198 Moderation Voter
13 days 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 12 days ago
Edited 12 days 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 198 — 12d
Ad
Log in to vote
0
Answered by 13 days 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