Shop Toucher Script/GUI

SO, what I’m trying to do is make a toucher, that when you touch it you get a car shop like gui thing so you can select a car to buy, BUT the script for the toucher isn’t working. Here is the script I’ve been using.

    function Touched(Part)
     	if Part then
    		if Part.Parent:FindFirstChild("Humanoid") and Part.Name == "Torso" then
    			if game.Players:FindFirstChild(Part.Parent.Name) then
    			if game.Players:FindFirstChild(Part.Parent.Name).PlayerGui:FindFirstChild("LucasChat") == nil then
 					    local NewGui = game.ReplicatedStorage.Guis.Chats.LucasChat:Clone()
    					NewGui.Parent = game.Players:FindFirstChild(Part.Parent.Name).PlayerGui
     				end
     			end
     		end
     	end
     end
    
    script.Parent.Touched:connect(Touched)

I’ve tried changing the scripts multiple times but it doesn’t work. Here is also the script I used in Local Script for the “VehicleCatalog” GUI.

 for A = 1,20 do
 	wait()
	script.Parent.BackFrame.BackgroundTransparency = 1-(A/20)
 end
 script.Parent:WaitForChild("Catalog")
 script.Parent:WaitForChild("BuyItem")
 script.Parent:WaitForChild("NotEnoughMoney")
 local BuyItem = script.Parent.BuyItem:Clone()
 script.Parent.BuyItem:Destroy()
 local NotEnoughMoney = script.Parent.NotEnoughMoney:Clone()
 script.Parent.NotEnoughMoney:Destroy()
 local ItemFrame = script.Parent.Catalog.CatalogFrame.Item:Clone()
 script.Parent.Catalog.CatalogFrame.Item:Destroy()
 local NewPlatform = game.ReplicatedStorage.ServerStats.VehicleCatalogModel:Clone()
 NewPlatform.Parent = workspace workspace.CurrentCamera.CameraType = "Scriptable"
 Ang = 0
 Exit = false
 Item = nil
 
 game.Players.LocalPlayer.Character.Humanoid.Died:connect(function()
	if NewPlatform then
 		NewPlatform:Destroy()
 	end
	if Item then
		Item:Destroy()
 	end
 	game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
 	game.Workspace.CurrentCamera.CameraType = "Custom"
 end)
 
 game:GetService("RunService").RenderStepped:connect(function()
 	if Exit == false then
 		Ang = Ang + 0.002
 		workspace.CurrentCamera.CoordinateFrame = CFrame.new(0,1018,0)*(CFrame.Angles(0,Ang,0)*CFrame.new(-5,0,25)*CFrame.Angles(math.rad(-30),0,0))
	end
 end)
 
 for A = 1,20 do
 	wait()
 	script.Parent.BackFrame.BackgroundTransparency = A/20
 end

 script.Parent.Catalog:TweenPosition(UDim2.new(0,0,0.3,0),1,1,1)
 
 local All = game.ReplicatedStorage.ServerStats.Vehicles:GetChildren()
 script.Parent.Catalog.CatalogFrame.CanvasSize = UDim2.new(0,0,0,(#All*50)+10)

 for A = 1,#All do
	local NewItemFrame = ItemFrame:Clone()
 	NewItemFrame.Parent = script.Parent.Catalog.CatalogFrame
 	NewItemFrame.Position = UDim2.new(0,5,0,-45+(A*50))
	NewItemFrame.Text = All[A].Name
	NewItemFrame.Thumbnail.Image = game.ReplicatedStorage.ServerStats.VehicleImages:FindFirstChild(All[A].Name).Value
	if All[A].Price.Value > 0 then
 		NewItemFrame.Price.Text = All[A].Price.Value
 	else
 		NewItemFrame.Price.Text = -All[A].Price.Value.."R"
		NewItemFrame.ImageLabel.Image = "http://www.roblox.com/asset/?id=347347731"
 	end
 	if game.ReplicatedStorage.PlayerStats:FindFirstChild(game.Players.LocalPlayer.Name).Vehicles:FindFirstChild(All[A].Name) then
 		NewItemFrame.Style = 3
	elseif game.ReplicatedStorage.PlayerStats:FindFirstChild(game.Players.LocalPlayer.Name).Gamepasses:FindFirstChild(All[A].Name) then
 		NewItemFrame.Style = 3
 	else
 		NewItemFrame.Style = 5
 	end
 	NewItemFrame.MouseEnter:connect(function()
		if Item ~= nil then
			Item:Destroy() 		
end
 		Item = game.ReplicatedStorage.ServerStats.Vehicles:FindFirstChild(All[A].Name):Clone()
		Item.Parent = workspace
		if All[A].Name == "Monotracer" then
 			Item:SetPrimaryPartCFrame(CFrame.new(NewPlatform.Main.Position+Vector3.new(0,2,-8))*CFrame.Angles(0,math.rad(180),0))
	elseif All[A].Name == "Golfcart" then 
			Item:SetPrimaryPartCFrame(CFrame.new(NewPlatform.Main.Position+Vector3.new(0,2,0)))
	elseif All[A].Name == "Jeep" then 
			Item:SetPrimaryPartCFrame(CFrame.new(NewPlatform.Main.Position+Vector3.new(0,2,0)))
		elseif All[A].Name == "Limo" then 
 			Item:SetPrimaryPartCFrame(CFrame.new(NewPlatform.Main.Position+Vector3.new(0,2,0)))
 		end
 	end)
 	NewItemFrame.MouseButton1Click:connect(function()
 		if All[A].Price.Value > 0 then --PP Purchase
			if script.Parent:FindFirstChild("BuyItem") == nil and script.Parent:FindFirstChild("NotEnoughMoney") == nil and NewItemFrame.Style == Enum.ButtonStyle.RobloxRoundDropdownButton then
 				local NewBuyItem = BuyItem:Clone()
 				NewBuyItem.SubFrame.TextLabel.Text = "Do you want to buy the vehicle "..All[A].Name.." for "..All[A].Price.Value.."PP?"
 				NewBuyItem.Visible = true			
		NewBuyItem.Parent = script.Parent
				NewBuyItem.SubFrame.Buy.MouseButton1Click:connect(function()
 				if game.ReplicatedStorage.PlayerStats:FindFirstChild(game.Players.LocalPlayer.Name).Money.Value >= All[A].Price.Value and All[A].Price.Value >= 0 then
 					game.ReplicatedStorage.ServerStats.ChangeMoney:FireServer(-(All[A].Price.Value),math.floor(math.sqrt(game.ReplicatedStorage.ServerStats.CurrentID.Value))+1337)
				game.ReplicatedStorage.ServerStats.BuyVehicle:FireServer(All[A].Name)
					NewBuyItem:Destroy()
					NewItemFrame.Style = 3
 				else
					local NewNotEnoughMoney = NotEnoughMoney:Clone()
 					NewNotEnoughMoney.Parent = script.Parent
 					NewNotEnoughMoney.Visible = true
 					NewBuyItem:Destroy()
 					NewNotEnoughMoney.SubFrame.Ok.MouseButton1Click:connect(function()
						NewNotEnoughMoney:Destroy()
				end)
				end
 			end)
 				NewBuyItem.SubFrame.Cancel.MouseButton1Click:connect(function()
 					NewBuyItem:Destroy() 				end)
 			end
	else --Gamepass Purchase
 			if script.Parent:FindFirstChild("BuyItem") == nil and script.Parent:FindFirstChild("NotEnoughMoney") == nil and NewItemFrame.Style == Enum.ButtonStyle.RobloxRoundDropdownButton then
 				local NewBuyItem = BuyItem:Clone()
				NewBuyItem.SubFrame.TextLabel.Text = "Do you want to buy the vehicle "..All[A].Name.." for "..-All[A].Price.Value.." Robux?"
			NewBuyItem.Visible = true			
			NewBuyItem.Parent = script.Parent
			NewBuyItem.SubFrame.Buy.MouseButton1Click:connect(function()
	    		game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer,361204620)
				end)
			NewBuyItem.SubFrame.Cancel.MouseButton1Click:connect(function()
 					NewBuyItem:Destroy()
 				end)
			end
 		end
 	end)
 end
 
 script.Parent.Catalog.Back.MouseButton1Click:connect(function()
	script.Parent.Catalog:TweenPosition(UDim2.new(0,-300,0.3,0),1,1,1)
 	wait(1)
 	for A = 1,20 do
		wait()
 		script.Parent.BackFrame.BackgroundTransparency = 1-(A/20)
 	end
	game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
 	game.Workspace.CurrentCamera.CameraType = "Custom"
 	if NewPlatform ~= nil then
 		NewPlatform:Destroy()
 	end
 	if Item ~= nil then
 		Item:Destroy()
 	end
 	for A = 1,20 do
 		wait()
	script.Parent.BackFrame.BackgroundTransparency = A/20
 	end
 	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("LucasChat") then
		game.Players.LocalPlayer.PlayerGui:FindFirstChild("LucasChat").Exit:FireServer()
 	end
 	script.Parent:Destroy()
 end)

If you THINK or KNOW how to fix this please, tell me. Thanks!

Please use the script tags to make it easier to read.

2 Likes

Hey, script2lu First thing I would like to recommend you to stop the use of Script.parent as its a bad behaviour.Also make sure to make your script be more organised for example start by Variables at the top and functions etc. This will help other users be able to help you and also will make it easy for us helping you read your scripts. What I would suggest you is to send an update on this script but an improved version which will make it easy for others to read and help you solve your problem.

2 Likes

Please format your script properly and cut out any unrelated info.

Right off the bat, unless your game is set to R6, dont use “Torso”, use “HumanoidRootPart” because most players don’t have a torso.

1 Like

Remove the if statement for part.name == “Torso” and that should work I think.

1 Like

I have fixed up the code for the Touched function.

HERE:

function Touched(Part)
	if (Part) then
		if (Part.Parent:FindFirstChild("Humanoid") and Part.Name == "Torso" or Part.Name == "LowerTorso" or Part.Name == "UpperTorso") then
			if (game:GetService("Players"):FindFirstChild(Part.Parent.Name)) then 
				if (game:GetService("Players"):FindFirstChild(Part.Parent.Name).PlayerGui:FindFirstChild("LucasChat")) == nil then 
					local NewGui	= game:GetService("ReplicatedStorage").Guis.Chats.LucasChat:Clone()
    				NewGui.Parent	= game:GetService("Players"):FindFirstChild(Part.Parent.Name).PlayerGui
				end;
			end;
		end;
	end;
end;
1 Like

Alright! I’ll try that. Thanks!

Thank you!! I’ll test it out. :smile:

Okay! Thank you, I’ll check it out.

I don’t agree with this. script.Parent often is exactly the best behavior because it means you can cut and paste Instances around in your workspace and not have to fix up the scripts that are attached to them because paths changed.

1 Like

I’ve tried out your script but it didn’t work. Thank you for helping anyways!

I changed “Torso” to “HumanRootPart” but it didn’t work. I think maybe it’s the GUI script. Thanks for helping though!

General comments: get in the habit of using early returns. Deeply indented code like this is painful to read. Instead of your original function, you can write it like this instead:

function Touched(Part)
  -- Uhh, this isn't even possible to begin with.  Why would you ever get Touched on a nil part?  You can probably even just delete this.
  if not Part then
    return
  end

  if not Part.Parent:FindFirstChild("Humanoid") or Part.Name ~= "Torso" then
    return
  end

  local Player = game.Players:FindFirstChild(Part.Parent.Name)
  if not Player then
    return
  end

  if Player.PlayerGui:FindFirstChild("LucasChat") then
    -- Player already has a gui, just return
    return
  end

  local NewGui = game.ReplicatedStorage.Guis.Chats.LucasChat:Clone()
  NewGui.Parent = Player.PlayerGui
end

Also, you say it’s not working but you didn’t say what isn’t working. Do you get an error? Does it not show up? Did you try running in the debugger? etc etc

Alright! I’ll change it to that script. Thank you.

its “HumanoidRootPart” not “HumanRootPart” :stuck_out_tongue:

OOp, wrote it wrong. I wrote "HumanoidRootPart’ in the script but I wrote “HumanRootPart” lol

I figured out what was wrong. The scripts were fine it was just the kind of avatar. It has to be R6 to be working. Not R15.