i’m trying to make a script that highlights objects. i followed this tutorial, but when i tried tweaking it, it throws an error. it’s telling me that the workspace has no position property. the reason why the error is thrown is beyond me, as i am not trying to check for a nonexistent ‘Position’ property in the workspace. how can i fix this?
local collection_service = game:GetService("CollectionService")
local player_service = game:GetService("Players")
local cool_tag = "interactable"
local max_dist = 25
local client = player_service.LocalPlayer
local character = client.Character or client.CharacterAdded:Wait()
local mouse = client:GetMouse()
local cooldown = false
local highlight = script:WaitForChild("interactable_outline")
mouse.Move:Connect(function()
if not mouse.Target then highlight.Adornee = nil return end
if (mouse.Target.Position - character.PrimaryPart.Position).Magnitude > max_dist then
highlight.Adornee = nil
return
elseif (mouse.Target.Parent.Position - character.PrimaryPart.Position).Magnitude > max_dist then
return
end
mouse.Button1Down:Connect(function()
if cooldown then return end
cooldown = true
print(`interacted with interactable object {mouse.Target}`)
-- add cooler logic here ..
task.wait(0.5)
cooldown = false
end)
if collection_service:HasTag(mouse.Target, cool_tag) then
highlight.Adornee = mouse.Target
return
elseif collection_service:HasTag(mouse.Target.Parent, cool_tag) then
highlight.Adornee = mouse.Target.Parent
return
end
highlight.Adornee = nil
end)
-- (mouse.Target.Position - character.PrimaryPart.Position).Magnitude > max_dist
it seems like its checking whatever targets parents position is, the target may be a a descendent from the workspace and it ends up checking for its parents position, which is the workspace
To be specific its Line 16 that is causing the error. Since Mouse.Target returns BasePart? which means it can be anything all you need to do is to make sure its a part and if it is it can continue through the rest of the script
Also i didnt see a point of adding else Mouse.Target.Parent
So i made my own version to test and this is the script that worked for me
local collection_service = game:GetService("CollectionService")
local player_service = game:GetService("Players")
local cool_tag = "interactable"
local max_dist = 25
local client = player_service.LocalPlayer
local character = client.Character or client.CharacterAdded:Wait()
local mouse = client:GetMouse()
local cooldown = false
local highlight = script:WaitForChild("interactable_outline")
mouse.Move:Connect(function()
if not mouse.Target then highlight.Adornee = nil return end
if not mouse.Target:IsA("Part") then return end
-- if it is not in the max dist radius
if (mouse.Target.Position - character.PrimaryPart.Position).Magnitude > max_dist then
highlight.Adornee = nil
return
end
mouse.Button1Down:Connect(function()
if cooldown then return end
cooldown = true
print(`interacted with interactable object {mouse.Target}`)
-- add cooler logic here ..
task.wait(0.5)
cooldown = false
end)
if collection_service:HasTag(mouse.Target, cool_tag) then
highlight.Adornee = mouse.Target
return
end
highlight.Adornee = nil
end)