function PlayerInteraction.new(client)
local self = setmetatable({
client = client
}, PlayerInteraction)
self.canInteract = false
self.canInteractDistance = 3
return self
end
function PlayerInteraction:update()
local player = self.client.Player
local playerCamera = player.Camera
if playerCamera then
local currentMouseHit = playerCamera:GetMouseRay()
local currentMouseHitPart = playerCamera:GetMouseHit()
local currentMouseHitPosition = currentMouseHit.Origin + (currentMouseHit.Direction * self.canInteractDistance)
local currentPosition = player.Character.PrimaryPart.Position
if currentMouseHitPart then
local currentMouseHitPartParent = currentMouseHitPart.Parent
local currentMouseHitPartParentHumanoid = currentMouseHitPartParent:FindFirstChildOfClass(“Humanoid”)
local currentMouseHitPartParentInteractable = currentMouseHitPartParent:FindFirstChildOfClass(“Interactable”)
if currentMouseHitPartParentHumanoid and currentMouseHitPartParentHumanoid.Parent == player.Character then
return
end
if currentMouseHitPartParentInteractable then
local currentMouseHitPartParentInteractableName = currentMouseHitPartParentInteractable.Name
local currentMouseHitPartParentInteractablePart = currentMouseHitPartParentInteractable.Part
if currentMouseHitPartParentInteractablePart then
local currentMouseHitPartParentInteractablePartPosition = currentMouseHitPartParentInteractablePart.Position
if (currentMouseHitPartParentInteractablePartPosition - currentPosition).magnitude < self.canInteractDistance then
self.canInteract = true
self.canInteractObject = currentMouseHitPartParentInteractable
return
end
end
end
end
self.canInteract = false
self.canInteractObject = nil
else
self.canInteract = false
self.canInteractObject = nil
end
end
return PlayerInteraction
I haven’t added the Interaction part yet but I’m just trying to see if this is a good way of going about it and if there are any ways it could be more efficient.
Thanks!
I think you can optimize the code that checks if the player is near an interactable object.
This is what i would do:
local function update()
playerInteraction.update()
end
function Client.initialize()
playerInteraction = PlayerInteraction.new(Client)
RunService.RenderStepped:Connect(update)
end
function PlayerInteraction.new(client)
local self = setmetatable({
client = client
}, PlayerInteraction)
self.canInteractDistance = 3
return self
end
function PlayerInteraction:update()
local mouseHit = self.client.Player.Camera:GetMouseRay()
local mouseHitPart = self.client.Player.Camera:GetMouseHit()
local mouseHitPartParent = mouseHitPart and mouseHitPart.Parent or nil
local mouseHitPartParentHumanoid = mouseHitPartParent and mouseHitPartParent:FindFirstChildOfClass(“Humanoid”) or nil
local mouseHitPartParentInteractable = mouseHitPartParent and mouseHitPartParent:FindFirstChildOfClass(“Interactable”) or nil
self.canInteract = false
self.canInteractObject = nil
if nearestInteractableObject and (nearestInteractableObject.Part.Position - self.client.Player.Character.PrimaryPart.Position).magnitude < self.canInteractDistance then
self.canInteract = true
self.canInteractObject = nearestInteractableObject
end
end
return PlayerInteraction