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

attempt to index nil with character?

Asked by 4 years ago

attempt to index nil with character, the script should work this way, u touch it and it anchores your head, it isnt really what i need to do completely

local part = workspace.ts4

part.Touched:wait()

player.Character.Head.Anchored = true

function onPartTouched()
    player.Character.Head.Anchored = true
end

part.Touched:connect(onPartTouched)

the ''player'' is blue below that i think its nil btw dont take down, i tried

1 answer

Log in to vote
0
Answered by
Dfzoz 489 Moderation Voter
4 years ago
Edited 4 years ago

player is a variable you have to set. You can get it by using game.Players:GetPlayerFromCharacter(model), which returns nil if the model isn't from a player character. It can be set in localscripts with local player = game.Players.LocalPlayer. BUT you are using a part.Touched event, so you don't need a player unless you want to mess with it too or to rule out npcs. Set a variable for the touched part:

player.Character.Head.Anchored = true

function onPartTouched(touch)
    if touch.Parent:findFirstChild("Humanoid") then --makes sure part is from a humanoid
        touch.Parent.Head.Anchored = true
    end
end

part.Touched:connect(onPartTouched)

0
thansk IIPedroI -3 — 4y
Ad

Answer this question