I am working on an horror game and the part I have an problem with is the GUI for the mainmenu. It works fine in Roblox Studio but not when I play online.
Script:
script.Parent.Touched:connect(function (hit) local player if hit.Parent:FindFirstChild("Humanoid") then player = game.Players:WaitForChild(hit.Parent.Name) end local gui = player:WaitForChild("PlayerGui"):WaitForChild("MainmenuGui") game.Workspace:WaitForChild(player.Name).Humanoid.JumpPower = 0 game.Workspace:WaitForChild(player.Name).Humanoid.WalkSpeed = 0 end)
Error: Infinite yield possible on 'Players.BeAHacker666.PlayerGui:WaitForChild("MainmenuGui")'
I found an "solution" on someone else's problem with the same issue that says I need to use localscript instead of serverscript. But then it doesn't even work in Studio.
I recommend you studying the code I added so you can understand how you can access guis in a player. I added comments all over the scripts to make you understand a little better. ^^
I'm not quite sure about what you want to do with the "MainmenuGui", but here is a way to access it:
In the part that are going to be touched add:
script.Parent.Touched:connect(function(hit) -- Brick Touched script.Disabled = true -- Disabling the script. (So It doesn't activate hundreds of times) if hit.Parent:FindFirstChild("Humanoid") then -- Checking for humanoid print("It is a humanoid!") -- Printing that it found the humanoid local player = game.Players:WaitForChild(hit.Parent.Name) -- Finds the player who touched player.PlayerGui.ThisWorks.Disabled = false -- Enables a localscript inside the player end -- Closes the if statement wait(1) -- Waits one second before activating the touched script script.Disabled = false -- Script is able to execute again end) -- Ends function
^^^^^^^^^^^^^^^^^^^^^^^^^^ So this is a server script which is inside the brick. It activates a localscript inside the player. But hey! We gotta make a localscript too! There are two ways in doing this:
1 - Making a serverscript which moves the script into the player (Will be able to change name of the script)
2 - Just adding a localscript into the PlayerGui (Can't change name of the script)
Option 1:
game.Players.PlayerAdded:Connect(function(plr) -- Fires when a player joins local Storage = game:GetService("ServerStorage") -- This is where the LocalScript is storaged local ThisWorks = Storage:WaitForChild("ThisWorks") -- This is my localscript ThisWorks.Parent = plr.PlayerGui -- My localscript's parent will be the player's playerGui print("Finished") -- Prints in console that the script is finished. end)
This is how my workspace looks like: https://gyazo.com/4a29dda5e09982919fe8b039ae36fc62
I have now moved my localscript into the player, and the script will be enabled whenever I touch the part.
The last part you have to do is to put something into the localscript. I just added some random stuff:
local player = game.Players.LocalPlayer -- Finds the localplayer print("It works? How nice!") -- Prints that the script is enabled after I touched the brick script.Disabled = true -- Disables the script (So I can fire it over and over again)
I hope you understand what I'm trying to show you. xD I know this is kinda messy but I'm sure you'll figure it out. You can now add:
local gui = player:WaitForChild("PlayerGui") local mainMenu = gui:WaitForChild("MainmenuGui")
And there you have it. You got access to your MainmenuGui. You can now do whatever you want with it. I hope you find this helping. ^^ Enjoy! ~WaterFoox