So atm I’m messing around in studio just scripting in an attempt to improve my knowledge which so far has gone surprisingly well. However I have ran into an issue of not being able to define a local players character and I have searched online but nothing works. Below is part of the script which I am using in an attempt to define a players character as it may just be a silly mistake I’m making.
local players = game:GetService("Players")
local player = players.LocalPlayer
local eventpart = script.Parent
local character = player:WaitForChild("Character")
– Note: Even though I have technically been scripting for 6 or so years and especially practicing in 2021, parts of 2022 and now 2023 I still have a poor understanding of scripting. Yes i know a good chunk basics but I for some reason can not progress.
Player object doesn’t have any child called Character. If you look into Explorer tab, you can see a service called Players, these is a player object with your name, if you use WaitForChild, you’re quering for a child of such object, which isn’t there and will never be there.
I understand this could be a little confusing, since you can access Workspace objects and their children by chaining calls game.Workspace.MyFolder.MyObject.MyPart, and so this would work same with game.Workspace.WaitForChild("MyFolder")..., but some objects do have a type members, type members aren’t Roblox objects but rather a variables of the object.
player.WaitForChild("Character") => Will be null, since your player object doesn’t have a child with name Character. player.Character => Will reference a model game.Workspace.Cloudy71 which is the character you’re looking for.
So I just tried this and it just dosent work. I’ve tried defining the players character in different games including ones which are published and so on but I keep getting the same error.
I have used both
local char = player.CharacterAdded:Wait()
-- and
local char = player.Character
Yet i keep getting the pratically same error of:
Workspace.MovementModifierService.JumpModifier.High.JumpModifierScript:5: attempt to index nil with 'CharacterAdded'
I have checked my other variables and there is absolutely nothing wrong with them. I’ve added a script loading system to only load scripts once a player has joined/loaded and yet again. Same error.
At this point I’m questioning whether its an issue with my studio however I’m unaware of this issue ever existing as by from what i can tell nearly nobody has ever reported on this issue/how to fix it.
I am now slowly dying on the inside as this issue is preventing me from writing character/humanoid based scripts which is slowly but surely making me die on the inside. Please help. Please.
Hm, strange, that should be the correct way to get the character inside a localscript, however you can’t have 2 variables with other things stored inside of it with the same name, so what you did there was wrong. I just don’t see what’s wrong with what I’ve wrote, however if
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
doesn’t work, try this
local player = game.Players.LocalPlayer
local character = game.Workspace:FindFirstChild(player.Name)
If i am not mistaken, this local script is parented to a part in workspace or something?
I believe that local scripts dont work under workspace with client-sided variables like this.
(I scripted this and it printed nothing after 10 seconds)
To say what other people are saying, you CANNOT get a LocalPlayer through a Script, you can ONLY get it through a Local Script.
Make sure that the script type youre using is a LocalScript, and that the script isnt parented to a part in workspace.
And again for this,
You cannot do plr:WaitForChild("Character"), but you can instead do
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
That code will still wait for the character to be added, and i would recommend then waiting for the Humanoid in the character, as the Humanoid gets added to the character once the player is off the loading screen.
I’ve been doing all this but I am confused on the only working in starter character scripts part as that would in my view not be reliable for what I want to script.
The last point also I have tried and I just tried the starter character scripts and i get the same error. I’m just going to completely wipe studio files from my computer and reinstall it in case something is broken in studio as at this point nothing is working.
I deleted all roblox studio related files on my computer and then reinstalled and for some reason it now works so I think it was to do with my studio after all.