Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can i make a player get a gui i have in a part if they touch it?

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 !

1 comment

0

I want to make it so if the player touches the part it gives the player the shop gui and opens it

3 answers

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!

33 comments

0

None of all the answers worked?!

0

What is your hierarchy?

0

Wut is that

0

Where is your script located and where is your Gui located.

0

Inside the part

0

Where is the ScreenGui located

0

also inside the part

0

its all located inside the part i want to be touched

0

Did you edit my answer at all? You might need to change line three to local gui = script.Parent.shop:Clone() , since your shop gui is named "shop" and not "Screengui". "ScreenGui" was an example.

0

i named the gui ScreenGui

0

Take a snapshot on where everything is located because I tested this script myself and It worked perfectly fine.

0

What is inside of the script? You must have made a typo or something.

0

script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local gui = script.Parent.ScreenGui:Clone() gui.Parent = player.PlayerGui end)

0

You probably have the outdated version of my answer in the script. Delete the contents and copy and paste my updated answer above. I made an accidental typo when I first submitted my answer. I apologize.

0

I already did that. ill do it again

0

I literally just tested it and it's working perfectly fine for me.

0

Not for me

0

Check the output and see if theres any errors.

0

No errors, It just won't work.. here is proofThat it should work http://prntscr.com/g71bzz

0

Well my answer definitely works. It might be because of your character or something. Did you use any custom character scripts?

0

Nope.. http://prntscr.com/g71cw8 im in R15 tho i don't think but maybe it has something to do with this? http://prntscr.com/g71d30

0

You have CanCollide turned off. You need to turn it on for it to work.

0

There is something wrong, if i click play the block just disappears

0

Is it Anchored?

0

yep

0

I don't know then. My script works 100%. Something in your game is screwing it up. Sorry.

0

Can you make it a model and send me the link?.

0

I guess

0

works..

0

Im so dumb the gui i had wasn't enabled?!?!?! Grrrr

0

Lol. Please accept my answer as I went through all this trouble to have it work for you,

--Sorry I forgot the Humanoid part!

Instructions

#1. Place the GUI in the part.

#2. place the script in the part

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)

6 comments

0

That's incorrect. You need to use GetPlayerFromCharacter. You cannot index LocalPlayer from a normal script in the workspace.

0

Or you atleast need to get the player who touched the part.

0

Are you trying to make mine sound like it doesn't work so you could get upvoted?!

0

now I made it where if it finds the humanoid that touched the part then it opens it for the localplayer.

0

That is not the reason. The reason was is because your answer wouldn't work if I beginner tried to use it. Again, LocalPlayer can only be indexed from a LocalScript. Not a server script.

0

*A beginner

This discussion is preserved as it was. New answers, comments, and votes are closed.