Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can't I retrieve the character from a fired server event?

Asked by 3 years ago
Edited 3 years ago

When you fire a server, the first parameter is the player. The player that fired the server exists in game.players, and game.workspace. With this knowledge, this script should work. Why does it not work? The error that I recieve from this is, "Infinite yield possible on 'Workspace:WaitForChild("Instance")'"

bb.OnServerEvent:Connect(function(player) local pl = game.Workspace:WaitForChild(player).Name game.ReplicatedStorage.Male:Clone() game.ReplicatedStorage:FindFirstChild("Male").Parent = game.Workspace.player.name game.Workspace.pl.Male.ball1.CFrame = CFrame.new(game.Workspace.pl.HumanoidRootPart.Position.X - 0.351, game.Workspace.pl.HumanoidRootPart.Position.Y - 1.325, game.Workspace.pl.HumanoidRootPart.Position.Z - 0.691) game.Workspace.pl.Male.ball2.CFrame = CFrame.new(game.Workspace.pl.HumanoidRootPart.Position.X + 0.326, game.Workspace.pl.HumanoidRootPart.Position.Y - 1.325, game.Workspace.pl.HumanoidRootPart.Position.Z - 0.691) game.Workspace.pl.Male.pp.CFrame = CFrame.new(game.Workspace.pl.HumanoidRootPart.Position.X, game.Workspace.pl.HumanoidRootPart.Position.Y - 0.925, game.Workspace.pl.HumanoidRootPart.Position.Z - 1.531) * CFrame.Angles(0, math.rad(90), 0) game.Workspace.pl.Male.DH.CFrame = CFrame.new(game.Workspace.pl.HumanoidRootPart.Position.X, game.Workspace.pl.HumanoidRootPart.Position.Y - 0.923, game.Workspace.pl.HumanoidRootPart.Position.Z - 2.547) * CFrame.Angles(0, math.rad(-90), 0) game.Workspace.pl.Male.FS.CFrame = CFrame.new(game.Workspace.pl.HumanoidRootPart.Position.X, game.Workspace.pl.HumanoidRootPart.Position.Y - 0.925, game.Workspace.pl.HumanoidRootPart.Position.Z - 2.051) * CFrame.Angles(0, math.rad(-90), 0) game.Workspace.pl.Male.pp.Color = game.Workspace.pl["Body Colors"].TorsoColor game.Workspace.pl.Male.ball1.Color = game.Workspace.pl["Body Colors"].TorsoColor game.Workspace.pl.CMale.ball2.Color = game.Workspace.pl["Body Colors"].TorsoColor end)

0
Please use the proper code block. They look like this: ~~~ Dovydas1118 1495 — 3y

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

WaitForChild takes string

WaitForChild accepts string as a first parameter while you passed Player which is an instance, from your script, which you should next time put in a code block which is a blue Lua button and should be inside of the ~~~~~~~~~~~~~'s, you want to get his Character. You put Name after the WaitForChild which would return name of the instance that WaitForChild returned, all you have to do is change it to local pl = game.Workspace:WaitForChild(player.Name).

Better solution of getting character

Every Player has property called Character, it's property which leads to character of the player. If the property is nil, it means that the character has not spawned yet but you can fix it using CharacterAdded event which fires when character of this player is added. You would first check if player's character is not nil like this:

bb.OnServerEvent:Connect(function(player)
    local pl = player.Character
    ...

If current character of the player does not exist, it will be nil, this is why you will have to use or statement and the event:

bb.OnServerEvent:Connect(function(player)
    local pl = player.Character or player.CharacterAdded:Wait()
    ...

If Character is nil, it will go onto the value after the or statement which in this case is a yield which will wait until CharacterAdded fires and will return what the function returns, in this case it's the Character.

Other Issues

Variables... Those are basics of Lua which you should know... why do you have variable pl and then you are doing game.Workspace.pl?.. This won't work, it will look for Instance named pl which does not most likely exist, your pl variable is already define so all you have to do is pl.HumanoidRootPart.CFrame....

Minor stupidity

Why... why are you using FindFirstChild and then instantly indexing it with Male? Read this article, it explains why is it stupid, shortly, it's because point of FindFirstChild is that you can check if given Instance has child inside of it named like this and if not, it won't error like with indexing but will just return nil which you can then check if the object exists. You are simply instantly indexing it with Parent. This FindFirstChild server no purpose and if the Male is nil, it Will error same like with indexing so change that to game.ReplicatedStorage.Male.Parent.

Sources

CharacterAdded

WaitForChild

FindFirstChild

Bad FindFirstChild + WaitForChild habits

Ad

Answer this question