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)
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
hey bends! there u are helping me once again. thank u so much 4 helping!
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:
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)
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)