[E TO ROB] Am I doing anything wrong with this code?

Store rob code (E TO ROB)
I would like to know

local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')


local Reward = ReplicatedStorage:WaitForChild('Reward')
local Register = workspace:WaitForChild('Register')
local RobPart = Register:WaitForChild('RobPart')

local HumanoidRootPart = Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')

local function CheckDistance(Subject)
	local Mag = (HumanoidRootPart.Position - Subject.Position).Magntiude
	
	if Mag < 15 then
		return
	end
end


UserInputService.InputBegan:Connect(function(Input, Typing)
	if Typing then return end
	if Input.KeyCode == Enum.KeyCode.E or Input.KeyCode == Enum.KeyCode.ButtonY then
		
		if CheckDistance(RobPart) then
			Reward:FireServer(RobPart.Reward.Value)
			
			local PlayerGui = Players.LocalPlayer:FindFirstChild('PlayerGui')
			local Reward = PlayerGui.Gui.Main:WaitForChild('Reward')
			
			if (Reward) then
				
				Reward.Visible = true
				Reward.Description = (RobPart.Reward.Value .. '+ Cash Instead')
				
				delay(3,function()
					Reward.Visible = false
					end)
			end
			
		end
	end
	
end)

local EPrompt = script.EPrompt


local Event
RunService.RenderStepped:Connect(function()
	if CheckDistance(RobPart) then
		
		if (Event) then
			Event = nil
			Event:Disconnect()
			
		end
		
		local NewPrompt = EPrompt:Clone()
		NewPrompt.Parent = workspace.CurrentCamera
		NewPrompt.Adornee = RobPart
		
	else
		
			for i,key in ipairs(workspace.CurrentCamera:GetChildren()) do
				if key:IsA('BillboardGui') then
					
					key:Destroy()
					
				end
			end
		
	end
end)


Iā€™m not entirely sure what you mean by ā€œAm I doing anything wrong,ā€ however, it does seem like your function ā€œCheckDistanceā€ isnā€™t going to work as you expect.
Currently, CheckDistance doesnā€™t return anything Mag >= 15 or returns nil if Mag < 15. I think you might want it to return ā€˜trueā€™ or ā€˜falseā€™ based on the outcome.

Works but keeps cloning the GUI :confused:

local EPrompt = script.EPrompt
EPrompt.Enabled = false

local Event
RunService.RenderStepped:Connect(function()
	if CheckDistance(RobPart) then
		
		if (Event) then
			Event = nil
			Event:Disconnect()
			
		end
		
		local NewPrompt = EPrompt:Clone()
		NewPrompt.Parent = workspace.CurrentCamera
		NewPrompt.Adornee = RobPart
		
		NewPrompt.Enabled = true
		
	else
		
			for i,key in ipairs(workspace.CurrentCamera:GetChildren()) do
				if key:IsA('BillboardGui') then
					
					key:Destroy()
					
				end
			end
		
	end
end)


Youā€™re runnning NewPrompt = EPrompt:Clone() every render step. Take the :Clone() out of the RenderStepped event to stop cloning it every frame.

1 Like