I am making a game where you can select a person based on a GUI to battle. When you click on the battle button, it should retrieve the default request GUI from the storage and send it to the player you want to battle.
The problem is that the GUI does not actually get sent to the player's GUI. The error I am getting is: "Infinite yield possible on 'Players.Player2:WaitForChild("PlayerGui")'" when testing with two people.
Is there anyway to fix this so that the Request GUI is properly sent to the desired player's GUI?
The Script is a LocalScript inside a button.
local id = script.Parent.Parent:WaitForChild("PlayerID") -- Player's ID local text = script.Parent:WaitForChild("Battletext") local searching = game:GetService('Players').LocalPlayer:WaitForChild("WaitingForPlayer") -- bool value that identifies if you are waiting for the player to accept or decline the request. local Request = game.ReplicatedStorage.DefaultRequest local localgui = game:GetService('Players').LocalPlayer:WaitForChild("PlayerGui") local localplayerid = game.Players.LocalPlayer.UserId local lookfor = game.ReplicatedStorage.RemoteEvent local lookForGameNextClick = true local player = game:GetService("Players"):GetPlayerByUserId(id.Value) script.Parent.MouseButton1Click:connect(function() print(id.Value) print(localplayerid) if id.Value == localplayerid then script.Disabled = true text.Text = "Can't Do That" wait(2) script.Disabled = false text.Text = "BATTLE" else if searching.Value == false then text.Text = "Cancel" searching.Value = true print("searching") sendmessage() ---------------------------------------- return end if searching.Value == true and text.Text == "Cancel" then text.Text = "BATTLE" searching.Value = false print("canceled") return else text.Text = "Waiting!" -- waiting for player to accept or decline script.Disabled = true wait(2) text.Text = "BATTLE" script.Disabled = false end end end) function sendmessage() local message = Request:Clone() if player then print("Worked") local guie = player:WaitForChild("PlayerGui") message.Parent = guie else print("Player with userId 1 is not in this server!") end end
You are trying to access someone else's PlayerGui
from a LocalScript
. You cannot do that. Furthermore, if your game is FE, it wouldn't even replicate.
What you should do instead is make clicking the button fire a remote handled by a server script, which then, clones the request gui to the target's PlayerGui
.
In your script:
function sendmessage() game.ReplicatedStorage.RequestRemote:FireServer(player) end
In a server script:
local requestRemote = Instance.new("RemoteEvent", game.ReplicatedStorage) requestRemote.Name = "RequestRemote" local Request = game.ReplicatedStorage.DefaultRequest requestRemote.OnServerEvent:Connect(function(plr, plr2) local msg = Request:Clone() msg.Parent = plr2:WaitForChild("PlayerGui") end)
Make sure to also edit the DefaultRequest
gui handler so that it's compatible with FE and RemoteEvents.