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

hit.Parent not working, took 8 month break, and now my code won't work, what is wrong?

Hello, My name is Mpire, and I tried to make this script for a shop gui. local gui = script.Parent.ShopGui wait(1) script.Parent.Touched:Connect(function(hit) local PlrName = hit.Parent.Name local Plr = game.Players.PlrName local clone = gui:Clone() clone.Name = "ShopClone" clone.Parent = Plr.Playergui

end) I am somewhat advanced at scripting, and i've been doing this for a long time, and i took a 8 month break from roblox, and now all of a sudden, this doesn't work... And here's the error " 13:11:30.584 - PlrName is not a valid member of Players "

1 comment

0

AdD a CoDe BlOcK

6 answers

    local gui = script.Parent.ShopGui
wait(1)
script.Parent.Touched:Connect(function(hit)
	local PlrName = hit.Parent.Name
	local Plr = game.Players.PlrName
	local clone = gui:Clone()
	clone.Name = "ShopClone"
	clone.Parent = Plr.Playergui
	
	
end)

3 comments

0

Sorry about that, forgot to add code block...

0

try game.Players.LocalPlayer.PlrName

1

You can edit your original post.

All the other answers are bad, and there may be many mistakes.

Try this script:

local Gui = script.Parent:WaitForChild("ShopGui")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --Checks if hit parent is player(not part or brick)
		local Char = hit.Parent
		local Player = game:GetService("Players"):GetPlayerFromCharacter(Char)
		local ClonedGui = Gui:Clone()
		ClonedGui.Parent = Player:WaitForChild("PlayerGui")
		ClonedGui.Name = "Cloned" --Your cloned gui name here
	end
end)

So here is your script. I will explain your errors.

local Gui = script.Parent.ShopGui
wait(1)
script.Parent.Touched:Connect(function(hit)
	local PlrName = hit.Parent.Name
	local Plr = game.Players.PlrName
	local clone = gui:Clone()
	clone.Name = "ShopClone"
	clone.Parent = Plr.Playergui
end)
  1. In your code second line useless, so you can remove that
  2. I recommend you to use if hit.Parent:FindFirstChild("Humanoid"). That will check if the brick hited by player
  3. If you have variable with player's name and you want to get the player from Players service you need to use game.Players:FindFirstChild(PlrName). If you use game.Players.PlrName game will think that you want to get the player with "PlrName" name.

P.s I am sorry for my bad english.

5 comments

0

Please explain your code in your answer. Otherwise, OP won't learn anything.

0

i only fixed his error nothing else

0

@ Darkwolfgameryr you didnt fixed that. What's means game.Players.LocalPlayer.PlrName? Really?

0

@lunatic5 O'key.

0

oh right misread the error

You can't use .PlrName when looking for a specific player, in fact, you can't use any variables after a dot. To reference a variable, you must use square brackets. That would make your script look like this:

local gui = script.Parent.ShopGui
-- took out the wait, as it wasn't necessary
script.Parent.Touched:Connect(function(hit)
    local PlrName = hit.Parent.Name
    local Plr = game.Players[PlrName]
    local clone = gui:Clone()
    clone.Name = "ShopClone"
    clone.Parent = Plr.Playergui
end)

Another useful thing that square brackets can do is reference models with spaces in their name. This can be very helpful and I suggest you keep it in mind. An example of this would be if you were making a tycoon and added a coal dropper. If you wanted to name it coal dropper with a space to make it look neater in your eyes, you could, it would just require a different code.

Hope I helped. -Cmgtotalyawesome

I think the reason for the error is that studio is now subjected to Filtering Enabled due to the accurate studio update and that you are trying to look for a string that is a child of game.Players rather than a player object. You could have done this to avoid the error by doing:

local plrName = hit.Parent.Name
local plr = game.Players[plrName]

--or use find first child

local plrName = hit.Parent.Name
local plr = game.Players:FindFirstChild(plrName)

Correct me if I'm wrong but I think you are using a server script, which is fine, but you can easily achieve the same goals by using a local script.

--I placed this local script inside the shopGUI
local plr = game.Players.LocalPlayer
workspace.ShopPart.Touched:Connect(function(hit)
	if hit and hit:IsDescendentOf(plr.Character) then
		local shopFrame = script.Parent.Frame
		shopFrame.Visible = true 
	end
end)

you can have the gui inside the starter gui and there really isnt any need for cloning as long as you have one frame that encompasses every other GUI element of the shop gui

Hi, is the gui an actual GUI ? if so i Would say you need to do a clicked event, like MouseButton1Down:Connect

touched event is if the actual characters body part was to touch it, or another part for that matter. Let me know! I've never known to use a touched event for a gui

you gotta do this bcz u diddnt add LocalPlayer

 local gui = script.Parent.ShopGui
	wait(1)
	script.Parent.Touched:Connect(function(hit)
	    local PlrName = hit.Parent.Name
	    local Plr = game.Players.LocalPlayer.PlrName
	    local clone = gui:Clone()
	    clone.Name = "ShopClone"
	    clone.Parent = Plr.Playergui
	 	 	end)

if it still wrong dm me

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