Hi folks! Today I have something to show and would like to ask why exactly it happens. I’ll demonstrate.
So we have this small script
for _,v in pairs(game.Workspace:GetDescendants()) do
if v:IsA('BasePart') then
local isplr = false
for _,p in pairs(game.Players:GetPlayers()) do
if v:IsDescendantOf(p.Character) then
isplr = true
end
end
if not isplr then
print('"' ..v.Name ..'" is not in a player character')
end
end
end
the code is self explanatory, go through all BaseParts in workspace and tell me if they’re not a descendant of a player’s character. now we put this in ServerScriptService and see if it works…
aaand it works as expected, but here’s the part that bothers me, if we now turn this into a localscript and put it in StarterPlayerScripts and uhhh
and nothing, if you change the code in this manor:
for _,v in pairs(game.Workspace:GetDescendants()) do
if v:IsA('BasePart') then
local isplr = false
for _,p in pairs(game.Players:GetPlayers()) do
if v:IsDescendantOf(p.Character) then
isplr = true
end
end
if isplr then
print('"' ..v.Name ..'" is in a player character')
end
end
end
then uhhhhhhh
now it says that EVERYTHING is a descendant of a player character. The solution to fix this is to either put the code in StarterCharacterScripts or to add the following before the block of code
game.Players.LocalPlayer.CharacterAdded:Wait()
This works fine for what i want to do in my case, but I’d like to know why this exactly happens or if this is intentional or not.
That’s all, have a nice day