Press "E" To Rob UI Script Not Working (Need Help)

So i am trying to make a ATM players can rob to get some cash, and i ran into problems with 2 scripts. I got feedback and someone helped me fixed the first one, however the UI script is acting up.

local inputservice = game:GetService("UserInputService")

inputservice.InputBegan:connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Door in pairs(workspace.Shops:GetChildren()) do
				local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
				if Mag <= Door.Range.Value then
					Door.Event:FireServer()
					break
				end
			end
		end
	end
end)


while true do
	script.Parent.ImageLabel.Visible = false
	for _,Door in pairs(workspace.Shops:GetChildren()) do
		local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		if Mag <= Door.Range.Value then
			local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(Door.Center.Position)
			script.Parent.ImageLabel.Visible = true
			script.Parent.ImageLabel.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
			break
		end
	end
	wait()
end

Here are the Output logs of the error:

[19:25:34.586 - HumanoidRootPart is not a valid member of Model] 
(rbxopenscript://www.dummy.com/dummy? 
scriptGuid=%7B3362D3D0-1795-46E1-9D2C- 
801ABF5B2B60%7D&gst=2#21)

19:25:34.588 - Stack Begin

[19:25:34.589 - Script 
'Players.AidanPlaysYT_Real.PlayerGui.PutinStarterGui.LocalScript', 
Line 21](rbxopenscript://www.dummy.com/dummy? 
scriptGuid=%7B3362D3D0-1795-46E1-9D2C- 
801ABF5B2B60%7D&gst=2#21)

19:25:34.590 - Stack End

What should i do?

You are calling PlayerGui from workspace

2 Likes

wait where did i do that? I forgot.

And the lines that are erroring:
'Players.AidanPlaysYT_Real.PlayerGui.E To Rob (ATM).LocalScript', Line 21
ResponseBody:
HumanoidRootPart is not a valid member of Model


'Workspace.Shops.Store.ShopMain', Line 7
ResponeBody:
PutinStarterGui is not a valid member of PlayerGui

1 Like

can you also help me with the UI script. It is acting up.

local inputservice = game:GetService("UserInputService")

inputservice.InputBegan:connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Door in pairs(workspace.Shops:GetChildren()) do
				local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
				if Mag <= Door.Range.Value then
					Door.Event:FireServer()
					break
				end
			end
		end
	end
end)


while true do
	script.Parent.ImageLabel.Visible = false
	for _,Door in pairs(workspace.Shops:GetChildren()) do
		local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		if Mag <= Door.Range.Value then
			local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(Door.Center.Position)
			script.Parent.ImageLabel.Visible = true
			script.Parent.ImageLabel.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
			break
		end
	end
	wait()
end

I don’t know what is wrong with it but something is happening with the script.

Can you elaborate on what’s wrong with it?

Judging by how you wrote this code. I’m going to assume you aren’t using any sort of system that wouldn’t reload your client code every time the player dies. This may not fix it but I suggest not putting HumanoidRootPart in the inputBegan event.

local playerObject = game.Players.LocalPlayer

local characterObject = playerObject.Character or playerObject.CharacterAdded:Wait()

local humanoidRootPart = characterObject:WaitForChild("HumanoidRootPart")

and just use that when getting the humanoid root part.

Edit: Oh, that is actually the problem. In a whilte true do your client loads before your humanoidrootpart is created. Add that to the top of your script and it should work.

2 Likes

Okay, Tysm! That worked. I am not very good at scripting, so that was a big help!