Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

0

besides this you can use player added function.

0

thanks ill see if it works

2 answers

✓ Accepted answer

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 :)

4 comments

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.

0

That is not related to getting the Player so you are gonna have to ask a new question for that

0

okay

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.