Local gui is shown to all players

For some reason, the local script make visible shopgui for all players on the server.

shop localscript.

local shop = script.Parent
shop.Visible = false
local openShop = game.Workspace.ShopPart
local replStorage = game:GetService('ReplicatedStorage')
local closShop = script.Parent.Xbutton
local buy = script.Parent.BuyButton
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local canShop = true
local buyPickaxe2 = replStorage:WaitForChild('RemoteEvent2')

local function showMenu(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if canShop and humanoid then
		humanoid.WalkSpeed = 0
		canShop = false
		shop.Visible = true
	end
end

local function closeMenu()
	localPlayer.Character.Humanoid.WalkSpeed = 16
	shop.Visible = false
	wait(2)
	canShop = true
end

local function buyTool2()
	buyPickaxe2:FireServer()
	print('Event fired')
end


buy.MouseButton1Click:Connect(buyTool2)
closShop.MouseButton1Click:Connect(closeMenu)
openShop.Touched:Connect(showMenu)

Thank you.

1 Like

Is your script a Script? Because LocalScripts are triggered for the Client while the Script triggers in the server.

No, its LocalScript
image

Try this:

local shop = script.Parent
shop.Visible = false
local openShop = game.Workspace.ShopPart
local replStorage = game:GetService('ReplicatedStorage')
local closShop = script.Parent.Xbutton
local buy = script.Parent.BuyButton
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local canShop = true
local buyPickaxe2 = replStorage:WaitForChild('RemoteEvent2')

buy.MouseButton1Click:Connect(function()
	buyPickaxe2:FireServer()
	print('Event fired')
end)

closShop.MouseButton1Click:Connect(function()
	local function closeMenu()
		localPlayer.Character.Humanoid.WalkSpeed = 16
		shop.Visible = false
		wait(2)
		canShop = true
	end

end)

openShop.Touched:Connect(function()
	localPlayer.PlayerGui.shop.Visible = true
end)
1 Like

hmm… no, still opens 2 guis. But thanks for trying to help

Hey there, would you like to elaborate what exact behavior you want to achieve? I’m trying to understand what you want to do without success.

I need that when a player stands on part, ShopGui will open only for this player, but for some reason the gui will open for all players

1 Like

OH WAIT I FIGURED OUT.
The reason why it shown to all guis because the Touched:Connect(showmenu) thing. Causing it to show all for players. Use remote events for Server to Client. This is how:

Add a remoteevent to ReplicatedStorage and Rename it ‘ShowShopGui’
Add a New script to ServerScriptService

local RemoteEvent = game.ReplicatedStorage.ShowShopGui
local openShop = game.Workspace.ShopPart

openShop.Touched:Connect(function(hit)
	local Players = game:GetService("Players")
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		RemoteEvent:FireClient(Player)
	end
end)

Then Update the LocalScript for the Shop Gui for the client

local shop = script.Parent
shop.Visible = false
local openShop = game.Workspace.ShopPart
local replStorage = game:GetService('ReplicatedStorage')
local closShop = script.Parent.Xbutton
local buy = script.Parent.BuyButton
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local canShop = true
local buyPickaxe2 = replStorage:WaitForChild('RemoteEvent2')
local ShowShopGui = game.ReplicatedStorage.ShowShopGui

ShowShopGui.OnClientEvent:Connect(function()
	local humanoid = Players.LocalPlayer.Character:FindFirstChild("Humanoid")
	if canShop and humanoid then
		humanoid.WalkSpeed = 0
		canShop = false
		shop.Visible = true
	end
end)


local function closeMenu()

	local humanoid = localPlayer.Character:FindFirstChild("Humanoid)
        if humanoid then
	         shop.Visible = false
	         wait(2)
	        canShop = true
        end     
end

local function buyTool2()
	buyPickaxe2:FireServer()
	print('Event fired')
end


buy.MouseButton1Click:Connect(buyTool2)
closShop.MouseButton1Click:Connect(closeMenu))

hope it helps.

2 Likes

The issue is that even if its a localScript, the part is in the workspace. Which means it detects any player’s touch.

You can simply fix the issue, with checking whether the player touched the part is the LocalPlayer

Here is the fixed script:

local shop = script.Parent
shop.Visible = false
local openShop = game.Workspace.ShopPart
local replStorage = game:GetService('ReplicatedStorage')
local closShop = script.Parent.Xbutton
local buy = script.Parent.BuyButton
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local canShop = true
local buyPickaxe2 = replStorage:WaitForChild('RemoteEvent2')

local function showMenu(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	
	-- Checking if the player that touched the part is the local player
	if canShop and humanoid and humanoid.Parent.Name == game:GetService("Players").LocalPlayer.Name then
		humanoid.WalkSpeed = 0
		canShop = false
		shop.Visible = true
	end
end

local function closeMenu()
	localPlayer.Character.Humanoid.WalkSpeed = 16
	shop.Visible = false
	wait(2)
	canShop = true
end

local function buyTool2()
	buyPickaxe2:FireServer()
	print('Event fired')
end


buy.MouseButton1Click:Connect(buyTool2)
closShop.MouseButton1Click:Connect(closeMenu)
openShop.Touched:Connect(showMenu)

I hope this helps :slight_smile:

2 Likes

Alright, thanks for the elaboration! Now let’s see what can we do. When you put a ScreenGui inside the StarterGui all players will receive the gui upon joining and that’s fine. We want to avoid that we can place the shop gui inside ReplicatedStorage/ServerStorage and by a script that is located inside the ServerScriptService we can check if the player is standing on the part or not, if the player is standing we can clone the gui and put it inside the certain player PlayerGui!

So let’s see how it works.

Server code:

local part: BasePart = path.to.part (ie: workspace.part)
local screenGui = path.to.screenGui (ie: ReplicatedStorage.gui/ServerStorage.gui)

local connection: RBXConnectionSignal
connection = part.Touched:Connect(function(h --[[ The player character part]]--)
        // Here find the humanoid and use Players to GetPlayerFromCharacter (ie local player = ...)
        local playerGui = player:FindFirstChild("PlayerGui")
        screenGui:Clone().Parent = playerGui
end)
-- Important in order to garbage collect the signal
connection:Disconnect()

Client code:

-- Do what you want here

local closeButton = path.to.closeButton
closeButton.MouseButton1Click:Connect(function ()
// Gui:Destroy() or if bought something you can also destroy the gui
end)

This is the idea of doing the desired behavior you want. You will have to change stuff according to your usage

1 Like

thank you very much for all your solutions, I tested them all and they work. Thanks again.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.