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

GetPlayerFromCharacter is not a valid member of model?

Asked by 7 years ago
Edited 7 years ago

Here is what it says in the output: "12:35:22.258 - GetPlayerFromCharacter is not a valid member of Model"

The code is here:

script.Parent.Touched:connect(function(otherPart)
    if not otherPart.Parent:WaitForChild("Humanoid") then end
    local player = otherPart.Parent:GetPlayerFromCharacter()
    player:Kick("Sucks to be you!")
end)

2 answers

Log in to vote
1
Answered by 7 years ago

The problem is that GetPlayerFromCharacter is a member of game.Players, not the model. Instead, you must do this:

script.Parent.Touched:connect(function(otherPart)
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
    if player then
        player:Kick("Sucks to be you!")
    end
end)

Note that you should also check to see if the character is a player before trying to kick them. This also renders the first check redundant (it had a few problems anyway).

Ad
Log in to vote
0
Answered by 7 years ago

GetPlayerFromCharacter

GetPlayerFromCharacter was supposed to be a function of Players, but nice try!

Here is the edited script:

script.Parent.Touched:connect(function(otherPart)
    if not otherPart.Parent:WaitForChild("Humanoid") then end
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) -- Access Players to call the GetPlayerFromCharacter function!!!
    player:Kick("Sucks to be you!")
end)

Don't forget where to access and call different built-in functions the next time you script!

If you have any problems, please leave a comment below. Thank you and I hope this will help you!

Answer this question