Trying to get player from clickdetector

i have a clickdetector in every players torso and i want a rope attaching both characters when one clicks the other. is there any way to get the player from the clickdetector?

local plrs = game:GetService("Players")
local ins = game:GetService("InsertService")

local plrs = game:GetService("Players")
local ins = game:GetService("InsertService")

game.Players.PlayerAdded:Connect(function(p)
	--game.StarterPlayer.LoadCharacterAppearance = false
	p.CharacterAppearanceLoaded:Connect(function(c)
		local hum:Humanoid = c.Humanoid

		--assign face
		local decal = c.Head:FindFirstChild("face")
		if decal then
			decal.Texture = "http://www.roblox.com/asset/?id=83017053"
		else
			local newDecal = Instance.new("Decal", c.UpperTorso)
			newDecal.Name = "manface"
			newDecal.Texture = "http://www.roblox.com/asset/?id=83017053"
		end

		


		--remove clothes
		for i, ac in c:GetDescendants() do
			if ac:IsA("Shirt") or ac:IsA("Pants") then
				ac:Destroy()
			end
		end

		--body color
		for i, part in c:GetDescendants() do
			if part:IsA("BasePart") or part:IsA("MeshPart") then
				part.Color = Color3.fromRGB(163, 162, 165)
			end
		end


		local cd = game.ReplicatedStorage.charCD

		local cdCopy = cd:Clone()
		cdCopy.Parent = c.HumanoidRootPart

		cdCopy.MouseClick:Connect(function(plr)
			local myChar = plr.Character
			local rope = Instance.new("RopeConstraint", myChar)
			local otherplr = game.Players:GetPlayerFromCharacter(cdCopy.Parent)
			local at0 = Instance.new("Attachment", myChar.RightLowerArm)
			local at1 = Instance.new("Attachment", otherplr.Character.HumanoidRootPart)
			rope.Attachment0 = at0
			rope.Attachment1 = at1
		end)

	end)
end)

The player is the plr argument of your cdCopy.MouseClick event. Perhaps the event isn’t triggered due to the humanoid root part “covering” the torso from being detected? If so then checking for root part clicks might be more reliable instead. Only one line needs to be modified:

cdCopy.Parent = c.HumanoidRootPart

This will also ensure no errors will occur for different body types, with the above scenario your script would break if the characters were R15 for example.

2 Likes

hey nyrion! ty 4 ur response. i tried what u said and made some modifications to the script. (edited up top). however, this part comes up with an error.

			local at1 = Instance.new("Attachment", otherplr.Character.HumanoidRootPart)

when i click the other player its not getting their character. im not sure if u can do it this way with clickdetectors in characters but i swear i saw it in plenty of games.

image

You already have the player and the character variable in your whole function, why not just use them (you called them p and c)

1 Like

You can do it this way just fine.

I assume this is on the server, correct?

You can access the player being clicked with this:

More specifically, the p variable.

And you can access the player who clicked with this:

More specifically, the plr variable.

So more specifically, instead of

local otherplr = game.Players:GetPlayerFromCharacter(cdCopy.Parent)

Just use

local otherplr = p

Your error (attempt to index nil) means that you tried to index (ex index “Character”) a variable that’s set to nil. That probably means your code didn’t set otherplr to anything.

1 Like

@BendsSpace @yoshicoolTV @NyrionDev

hey guys! tysm for ur help. it worked. i didnt think it would work if i used the p variable from the top. i thought it was referencing my own player and not the other. thank u guys so much!

image

code solution:

local plrs = game:GetService("Players")
local ins = game:GetService("InsertService")

game.Players.PlayerAdded:Connect(function(p)
	--game.StarterPlayer.LoadCharacterAppearance = false
	p.CharacterAppearanceLoaded:Connect(function(c)
		local hum:Humanoid = c.Humanoid

		--assign face
		local decal = c.Head:FindFirstChild("face")
		if decal then
			decal.Texture = "http://www.roblox.com/asset/?id=83017053"
		else
			local newDecal = Instance.new("Decal", c.UpperTorso)
			newDecal.Name = "manface"
			newDecal.Texture = "http://www.roblox.com/asset/?id=83017053"
		end

		


		--remove clothes
		for i, ac in c:GetDescendants() do
			if ac:IsA("Shirt") or ac:IsA("Pants") then
				ac:Destroy()
			end
		end

		--body color
		for i, part in c:GetDescendants() do
			if part:IsA("BasePart") or part:IsA("MeshPart") then
				part.Color = Color3.fromRGB(163, 162, 165)
			end
		end


		local cd = game.ReplicatedStorage.charCD

		local cdCopy = cd:Clone()
		cdCopy.Parent = c.HumanoidRootPart

		cdCopy.MouseClick:Connect(function(plr)
			local myChar = plr.Character
			local otherchar = p.Character
			local rope = Instance.new("RopeConstraint", myChar)
			rope.Visible = true
			rope.Thickness = .15
			local otherplr = p
			local at0 = Instance.new("Attachment", myChar.RightLowerArm)
			local at1 = Instance.new("Attachment", otherchar.HumanoidRootPart)
			rope.Attachment0 = at0
			rope.Attachment1 = at1
		end)

	end)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.