script.Parent.Touched:connect(function() script.Parent.shop:Clone().Parent = game.Players.LocalPlayer.PlayerGui print("done") end)
that is the not working code please help me !
You're using a Server script, which can't access the LocalPlayer. A Server script has no idea who the client is. Every location (Starterpack, PlayerGui, StarterCharacterScripts, StarterPlayerScripts, etc..) that a LocalScript can be placed becomes in some way a descendant or child of a user when your player loads. However, place a LocalScript in workspace and that script is no longer associated with any player, because it's a child of workspace. This, however, doesn't mean you can't get the player from the part you touched.
This goes in a normal/server script.
local part = script.Parent -- Use WaitForChild() on children -- Scripts load before objects online local shop = part:WaitForChild("shop") -- Use 'C'onnect part.Touched:Connect(function(hit) -- Make sure hit didn't suddenly get destroyed -- Hit is what hit the brick, like your leg. if hit and hit.Parent then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then local char = player.Character local humanoid = char:findFirstChild("Humanoid") local player_gui = player:findFirstChild("PlayerGui") -- Make sure we didn't reset on it and we don't already -- have the shop if humanoid and humanoid.Health > 0 and not player_gui:findFirstChild("shop") then local shop_clone = shop:Clone() shop_clone.Parent = player_gui end end end end)
This goes in a normal script inside of the part. You need to use GetPlayerFromCharacter, that way you can clone the GUI into their PlayerGui.
script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local gui = script.Parent.ScreenGui:Clone() gui.Parent = player.PlayerGui end) --Edit: Made a typo. My bad
If this helped, please upvote and accept answer! Thanks!
--Sorry I forgot the Humanoid part!
Instructions
local gui = script.Parent.GUINAME script.Parent.Touched:Connect(function(hit) if hit.Parent.Name == 'Humanoid' then gui:Clone().Parent = game.Players.LocalPlayer.PlayerGui elseif hit.Parent.Name ~= nil then gui:Remove().Parent = game.Players.LocalPlayer.PlayerGui end end)