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

Infinite yield on Players.Player1:WaitForChild("PlayerGui")?

Asked by 5 years ago

So, I made a script wich fires a bindable event whenever one player decides to challenge the other one to a game. The other one should recieve an invite, but i see this:

Infinite yield possible on 'Players.Player1:WaitForChild("PlayerGui")'

I know what it means, etc. But why does it show up when it actually exists? Bindable event is located inside the gui responsible for the invite. I did the code etc but what is wrong with this one?

local playername = nil
local player = nil
local Event = nil

script.Parent.MouseButton1Click:Connect(function()
    playername = script.Parent.Parent.TextLabel.Text
    player = game.Players:FindFirstChild(playername)
    print(player.Name.." Found")
    Event = player:WaitForChild("PlayerGui").ChallengeGUI.Challenge --This causes the error
    print("Challenged ".. playername)
    Event:Fire()
end)
0
To communicate between players, You need to use RemoteEvent located in ReplicatedStorage. Bindable Event can only work locally on one client (or internally on server). sleazel 1287 — 5y
0
I prefer RemoteEvent because RemoteEvent already have the thing "player" once you put it inside the function() thing cherrythetree 130 — 5y

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
5 years ago

Roblox does not support direct client to client communication. All events have to go through the server. Therefore you will need two separate RemoteEvents. First one will fire server with the challenge, and the other will fire respective client.

Lets start with the server script. Put this one in ServerScriptSercvice:

ReplicatedStorage = game:GetService("ReplicatedStorage")

--Create events here. You can also create them manually, by right-clicking on replicated storage.
ReceiveChallenge = Instance.new("RemoteEvent")
ReceiveChallenge.Name = "ReceiveChallenge"
ReceiveChallenge.Parent = ReplicatedStorage

ForwardChallenge = Instance.new("RemoteEvent")
ForwardChallenge.Name = "ForwardChallenge"
ForwardChallenge.Parent = ReplicatedStorage

function OnReceiveChallenge(player,challengedPlayer)
    print(player.Name .. " challenged " .. challengedPlayer.Name)
    ForwardChallenge:FireClient(challengedPlayer,player) --opposite order
end
ReceiveChallenge.OnServerEvent:Connect(OnReceiveChallenge)

Now for Your LocalScript. You have not provided the script section, where You are handling incoming event, so I took some liberty in providing an example:

Players = game:GetService("Players")
ReplicatedStorage = game:GetService("ReplicatedStorage")

ReceiveChallenge = ReplicatedStorage:WaitForChild("ReceiveChallenge")
ForwardChallenge = ReplicatedStorage:WaitForChild("ForwardChallenge")

script.Parent.MouseButton1Click:Connect(function()

    playerName = script.Parent.Parent.TextLabel.Text
    challengedPlayer = Players:FindFirstChild(playername)
    print(challengedPlayer.Name.." Found")

    ReceiveChallenge:Fire(challengedPlayer)
end)

--next function handles incoming challenges
function OnReceiveChallenge(playerWhoChallengedMe)
    print(playerWhoChallengedMe.Name .. " challenged me!")
    --update some GUI element with that message
end
FrowardChallenge.OnClientEvent:Connect(OnReceiveChallenge)
0
Thank you very much! I am not so advanced into scripting & development to know these things. Once again thank you so much! <3 DrewnoBrzozowe 24 — 5y
Ad

Answer this question