That’s what I used before, but my script returned errors after the char was replaced after death. I assumed it was because the variable didn’t change after the char changed, but maybe it’s a different issue then.
well what was the error? and how ya script looked like?
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local plrbody = char:WaitForChild("HumanoidRootPart")
It works fine until I jump off the map, at which point this error message shows. The same thing happens with ‘or char:WaitForChild(“Torso”)’, so it has to be to do with the Char changing, or maybe being temporarily unavailable and therefore messing up the variables.
why dont you use :FindFirstChild
instead? i yet don’t know why that happens… does it happen when u reset too? also just check if its not nil when using FindFirstChild? i was reading dev forum whom had the same problem maybe but it dont look helpfull devForumLink
That’s so weird. I used :FindFirstChild and it worked, but only if I put a wait() first.
wait(1)
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local plrbody = char:FindFirstChild("HumanoidRootPart")
hm i would agree… also quick question where is the local script located?
In a tool in StarterPack, could that be an issue?
Try this:
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local plrbody = char:WaitForChild("HumanoidRootPart") or char:WaitForChild("Torso")
plr.CharacterAdded:Connect(function(updatedChar)
warn("New character added!")
char = updatedChar
hum = char:WaitForChild("Humanoid")
plrbody = char:WaitForChild("HumanoidRootPart") or char:WaitForChild("Torso")
end)
If you don’t want the values removed, why don’t you add them into the Player instance?
That didn’t work. I thought about it and it could be because I have a while loop later on in the script?
Could you explain what you mean?
The character is a property of the player, not a child.
I recommend creating a separate function that controls whatever you’re trying to do, managing connections would be easier. Something like this:
local localChar
local function getBody(character)
localChar = updatedChar
hum = char:WaitForChild("Humanoid")
plrbody = char:WaitForChild("HumanoidRootPart") or char:WaitForChild("Torso")
end)
getBody(plr.Character or plr.CharacterAdded:Wait()) -- get it when the player first joins
pls.CharacterAdded:Connect(getBody)
I also saw from the thread that you were trying to do this from a tool, when the player died you were having issues. You could probably navigate it like this:
local function doControl(player)
local character
player.CharacterAdded:Connect(function(newCharacter)
character = newCharacter
print('New character assigned to variable', newCharacter)
end)
character = player.Character or player.CharacterAdded:Wait()
print('Initial variable value ', character
end
My apologies, I misunderstood what you mean. I thought you meant you had like instances for variables. Disregard what I said.
I’ll try both. Could you explain the second function? As in where would I use it vs the other function? I’m assuming localChar is the char variable.
Also, why would it work with :FindFirstChild(), but not :WaitForChild()?
Edit: It didn’t work with the functions provided.
It shouldn’t have worked with :FindFirstChild unless there was an actual child of the player called Character.
The second function was an example of how you would go about getting the character of another player if they died.
Could you elaborate on what you’re trying to do specifically?
Yeah, sorry, I should have been more specific. I was talking about char:WaitForChild(“HumanoidRootPart”) vs FindFirstChild(), and how this worked fine:
wait(1)
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local plrbody = char:FindFirstChild("HumanoidRootPart")
But this broke my script:
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local plrbody = char:WaitForChild("HumanoidRootPart")
I’m applying your function to a script which causes the player to be able to pick up items if they press ‘e’. I’m doing this via a coroutine and while loop later on in the script. Could this be an issue? I tried placing a .CharacterAdded inside the while loop as well but it didn’t help.
At the moment I get this error message immediately when I use your first function:
local plr = Players.LocalPlayer
local char
local hum
local plrbody
local function getBody(updatedChar)
char = updatedChar
hum = char:WaitForChild("Humanoid")
plrbody = char:WaitForChild("HumanoidRootPart") or char:WaitForChild("Torso")
end
getBody(plr.Character or plr.CharacterAdded:Wait())
plr.CharacterAdded:Connect(getBody)
Sorry for the late response, was busy.
First thing, that should have worked, doesn’t appear Either :WaitForChild() or :FindFirstChild(). Could you provide a .rbxl/rbxm file to see if I can reproduce it?
Second thing, I see what you mean now. Your script is in a tool, correct? Also, the e to pickup should only appear when the tool is equipped, right?
I’m thinking that you could go about controlling the tool in a different way. Instead of a localscript inside of the tool, you could do something inside of StarterPlayerScripts or StarterCharacterScripts. Let me know if you’re interested in this option and I’ll try my best to help you rewrite.
Anyway, try and see if this helps or gives another issue.
local players = game:GetService('Players')
local plr = players.LocalPlayer
local char, hum, plrBody
local function getBody(updatedChar)
hum = updatedChar:WaitForChild('HumanoidRootPart')
plrBody = updatedChar:WaitForChild('HumanoidRootPart') or char:WaitForChild('Torso')
char = updatedChar
end
getBody(plr.Character or plr.CharacterAdded:Wait())
plr.CharacterAdded:Connect(getBody)
I’ll put a Pastebin of the full script below. I made it a while ago when I was less proficient so I would definitely do things differently now. It works as intended though. I just want to know why your function (which in my opinion is a better and safer way of getting the character than what I’ve used) doesn’t work.
If you don’t see anything immediately obvious in that I could probably move some assets to another file to try and replicate it, but everything is currently in a group edit studio session, so could be quite hard.
Gotcha.
So essentially what you need the body for is the magnitude from the root part to the fruit?
To be completely honest, I am not too sure what the issue could be but instead of trying to figure out why it isn’t working I’d like to offer a little bit of a solution.
I’m thinking the issue might derive from this line:
return (plrbody.Position - a.Position).magnitude > (plrbody.Position - b.Position).magnitude
So let’s create a function that gets a new Vector3 which is equal to the player’s HRP position instead of using plrBody
local function getLocalPlayerBodyPosition() -- you can shorten this, I just like using descriptive names whenever possible
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
return character:WaitForChild('HumanoidRootPart').Position
end
Then whenever plrbody.Position
appears in your script, replace it with getLocalPlayerBodyPosition()
For example,
return (getLocalPlayerBodyPosition() - a.Position).magnitude > (getLocalPlayerBodyPosition() - b.Position).magnitude
See if that helps.
Thanks for all the help. I’ve implemented that into my script.
I found out the problem was to do with the Tool itself - although I think there were a number of other problems to do with getting the char after death which were solved by your function. I’m not entirely sure what the exact issue was but I’m thinking it was to do with it unequipping when the char changed. I followed your advice and removed the tool entirely and placed the script into StarterPlayerScripts and it worked.