So I am working on a Hotel game in which when you touch the computer, a GUI opens. The only problem is that the GUI doesn't only open for one person, it opens for the whole server!
Here are the scripts I used:
local GUI = script.Parent local part = game.Workspace.coolyoungpart part.Touched:Connect(function() game.StarterPlayer.Value.Value = 1 end)
local GUI = script.Parent local val = game.StarterPlayer.Value if val.Value == 1 then GUI.Enabled = true end if val.Value == 0 then GUI.Enabled = false end val.Changed:Connect(function() if val.Value == 1 then GUI.Enabled = true end if val.Value == 0 then GUI.Enabled = false end end)
local button = script.Parent local GUI = script.Parent.Parent.Parent button.MouseButton1Click:Connect(function() game.StarterPlayer.Value.Value = 0 end)
Yeah, and that's it.
Can somebody help me?
Hello dear friend! Let me help you with this problem.
First off, I don't suggest using IntValues
as a method of toggling.
Secondly, let me fix your scripts.
For your first one, you need to make sure the object touching the part is a Humanoid
.
Insert a RemoteEvent
and write:
local toggle = true script.Parent.Touched:Connect(function(hit) if hit.Parent:WaitForChild('Humanoid') then game.ReplicatedStorage.RemoteEvent:FireClient() end end)
Then, add a LocalScript
inside of your ScreenGui
and write in:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() script.Parent.Enabled = true end)
Lastly, insert a LocalScript
inside of the Close button of your GUI and write in:
script.Parent.MouseButton1Click:Connect(function() script.Parent.Enabled = false end)
RemoteEvents
are things that serve as a solution to the server-client side problem. To learn more, go here
I see what you are trying to do, but seems like you've going into more troubles than you need to!
To give a GUI to a player is fairly simple, you just clone it into the player's PlayerGui!
local gui = script.Parent.Parent -- function to give a playera gui local function giveGuiTo(player) -- we clone the original GUI and parent it to the Player's PlayerGui gui:Clone().Parent = player.PlayerGui end
It's simple as that! If you wish to use the Touch event, then listen to it, find the player, and call the function or even directly write the code!
local players = game:GetService("Players") -- The player service local gui = script.Parent.Parent -- function to give a playera gui local function giveGuiTo(player) -- we clone the original GUI and parent it to the Player's PlayerGui gui:Clone().Parent = player.PlayerGui en part.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") -- we make sure that it's a human by checking for a humanoid if(humanoid) then local character = humanoid.Parent -- if it is a player, then the humanoid's parent is the character! -- GetPlayerFromCharacter() gets the player from a character local player = players:GetPlayerFromCharacter(character) -- if there is indeed a player then if(player) then giveGuiTo(player) end end end)