Need help with a table

im not sure how to do this. im seeing if the character has the items from the parts table already and if they do then to return. im not v good w tables zzzz. ty!

here’s the issue:

	local parts = {
		mychar["Right Arm"].at0,
		mychar["Right Arm"].at1,
		mychar.charRope
	}
	
	if mychar:GetDescendants(parts) then return end

script:

local event = game.ReplicatedStorage:WaitForChild("ropeEvent")

event.OnServerEvent:Connect(function(plr, otherplr)
	local mychar = plr.Character
	local otherchar = otherplr.Character
	
	local parts = {
		mychar["Right Arm"].at0,
		mychar["Right Arm"].at1,
		mychar.charRope
	}
	
	if mychar:GetDescendants(parts) then return end
	
local rope = Instance.new("RopeConstraint", mychar)
rope.Name = "charRope"
rope.Visible = true
rope.Thickness = .15
	local at0 = Instance.new("Attachment", mychar["Right Arm"])
	at0.Name = "at0"
	local at1 = Instance.new("Attachment", otherchar.HumanoidRootPart)
	at1.Name = "at1"
rope.Attachment0 = at0
rope.Attachment1 = at1
rope.Color = BrickColor.new("Really red")
rope.Length = 5
rope.WinchResponsiveness = 1
rope.WinchEnabled = true

end)
1 Like

sorry, I don’t think I found the question. whats the issue???

1 Like

Get descendants returns an array of the descendant instances. It doesn’t take any parameters. If you’re every curious about how to use a function, you can search the docs for it and it has some good information and examples.

What you’re looking to do is check if one array is a sub array of the other. Luau and Roblox don’t have a built in way to do this, so you’ll need to do it manually.

I would create a loop to do this, and instead of getting all of the descendants just check the few you’re looking for. Basically, start by assuming all of the parts are in the character, then use a for loop to check if any of the parts aren’t in the character, and if you find any then correct the initial assumption:

-- Start by assuming all the parts are there (and later update this if we find an exception)
local hasParts = true

-- Loop through the array of parts
for _, part in ipairs(parts) do
    if not part:IsDescendentOf(mychar) then
        -- Found a part that's not in the character, so we decide they aren't all in the character
        hasParts = false
        break -- Exit the for loop
    end
end

-- Use the resulting value:
if hasParts then
    return
end
2 Likes

@purpledanx

hey bends! there u are helping me once again. thank u so much 4 helping! :slight_smile:

i understand it a lot more now. i just have an issue. i put this in a localscript so when i click a player it’ll fire and it’ll create the rope. but if my character has the rope already, then it will stop and not run the function. im just getting this error:

code:

local myplr = game.Players.LocalPlayer
local event = game.ReplicatedStorage:WaitForChild("ropeEvent")
local mouse = myplr:GetMouse()
local mychar = game.Players.LocalPlayer.Character

local parts = {
	mychar.charRope
}

mouse.Button1Down:Connect(function()
	
	for _, part in ipairs(parts) do
		if not part:IsDescendentOf(mychar) then
			local otherplr = game.Players:GetPlayerFromCharacter(mouse.Target.Parent)
			local NPChum = mouse.Target.Parent:FindFirstChild("Humanoid")

			if otherplr then
				event:FireServer(otherplr)
			end
			
		else
			return
		end
	end
end)

tysm 4 ur help once again :slight_smile:

1 Like

Ok, I see. So the problem this time is that you try and get the rope before it has been spawned so replace

local parts = {
	mychar.charRope
}

with

local parts = {
	"charRope"
}

and replace this

for _, part in ipairs(parts) do
		if not part:IsDescendentOf(mychar) then

with this

for _, part in ipairs(mychar:GetDescendents()) do
		if table.find(parts,part.Name) then
2 Likes

The problem is from how the script runs at the start of the game, but at the start of the game the character doesn’t have a child named “charRope” yet.

I’m not sure what kind of assumptions you can make about charRope existing (code should only use .Somthing if it knows that Something will exist).

I was also thinking that the code could be used in a slightly different way, while the code you have works for one part it wouldn’t work for multiple.

local myplr = game.Players.LocalPlayer
local event = game.ReplicatedStorage:WaitForChild("ropeEvent")
local mouse = myplr:GetMouse()




mouse.Button1Down:Connect(function()
	-- Get the current character
	local mychar = game.Players.LocalPlayer.Character
	-- Ignore the button click if the player doesn't have a character (ie is dead)
	if not mychar then return end

	-- The part names that should exist *as children* of the character
	local partNames = {
		"charRope"
	}
	local hasParts = true
	for _, partName in ipairs(partNames) do
		if not mychar:FindFirstChild(part) then
			print("Didn't find part "..partName.."!") -- Just for testing
			hasParts = false
			break
		end
	end

	-- Return if the character has the parts already
	if hasParts then return end

	local target = mouse.Target
	-- Small change so it works if they click on accessories too
	local maybeOtherChar = target:FindFirstAncestorOfClass("Model")
	local otherplr = game.Players:GetPlayerFromCharacter(maybeOtherChar)
	-- Will set to humanoid if a valid player character exists
	local NPChum = nil

	if otherplr then
		-- (Only use `maybeOtherChar` if you know it exists)
		NPChum = maybeOtherChar:FindFirstChild("Humanoid")
		event:FireServer(otherplr)
	end
	
end)
1 Like

@purpledanx @BendsSpace

absolutely perfect. thank u so much guys. works like a charm. i really appreciate the help. have a great day and gl w ur scripting! :slight_smile:

2 Likes

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