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

How do you use a non-localplayer in a script?

Asked by 4 years ago
Edited 4 years ago

I'm scripting a morpher so that when someone steps on it they morph into that character. I think it needs to be a regular script to do

script.Parent.Touced:Connect(function()

so i'm using a regular script, but i don't know how to address the player without using

local plr = game.Players.LocalPlayer

because that only works with local scripts.

0
besides this you can use player added function. hallmk7 50 — 4y
0
thanks ill see if it works bolterazia -5 — 4y

2 answers

Log in to vote
2
Answered by
Filipalla 504 Moderation Voter
4 years ago
Edited 4 years ago

Why it's better to do it this way:

Grainless_Bread's answer will work in most cases but this will work as long as Hit is a descendant of the Player whereas his only works if the Character is the immediate Parent of Hit

The Solution:

function getPlayerFromCharacter(descendant)
    for _, Player in pairs(game:GetService("Players"):GetPlayers()) do -- Iterates through a list of all Players
        if descendant:IsDescendantOf(Player.Character) then -- Checks if the Player the for loop is currently at's Character has Hit as a descendant
            return Player -- Returns the Player
        end
    end
end

script.Parent.Touched:Connect(function(Hit)
    local Player = getPlayerFromCharacter(Hit)
    if Player then
        --Insert your code here
    end
end)

Don't forget to mark my answer as the solution and upvote it if it answered your question :)

0
it worked! one problem: It only changed my pants, not the hair, body type or accessories, and the character im trying to morph into doesn't have a shirt (its scooby doo so its just pants a hat and skin tone) so i got the error "Shirt is not a valid member of Model" because the morph has no shirt in it. bolterazia -5 — 4y
0
That is not related to getting the Player so you are gonna have to ask a new question for that Filipalla 504 — 4y
0
okay bolterazia -5 — 4y
0
Also, don't forget to mark the answer that helped you the most as the solution so that people know this has been solved Filipalla 504 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Here you go

script.Parent.Touched:Connect(function(plr)  -- the plr between the parameters is a part that came in contact with the part.
local player = game:GetService("Players"):GetPlayerFromCharacter(plr.Parent)  

if player then 
    --enter script here

end
end)

Hope this helps

<3

Answer this question