/
ClickPoint_Client.client.lua
276 lines (213 loc) · 6.94 KB
/
ClickPoint_Client.client.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
-- ClickPoint
-- itzfuneyy
-- 10/11/2022 19:27
-- Constant for ClickPoint tag.
local CLICKPOINT_TAG: string = "ClickPoint"
-- Initialise CollectionService.
local CollectionService = game:GetService("CollectionService")
-- Local function to create RaycastParams.
local function _generateRayParams(): RaycastParams
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Whitelist
RayParams.FilterDescendantsInstances = CollectionService:GetTagged(CLICKPOINT_TAG)
RayParams.RespectCanCollide = true
return RayParams
end
local RayParams = _generateRayParams()
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local HOVERING_OVER_CLICKER = false
local HOVER_OBJECT = nil
-- Messy local function for generating a tooltip.
local function _generateTooltip(instance: BasePart)
local ui = Instance.new("ScreenGui")
ui.Name = "Tooltip"
local frame = Instance.new("Frame", ui)
frame.Size = UDim2.new(0,175,0,18)
frame.BackgroundTransparency = 1
local label = Instance.new("TextLabel", frame)
label.Size = UDim2.new(1,0,1,0)
label.Font = Enum.Font.Gotham
label.TextScaled = true
label.BackgroundTransparency = 1
label.TextXAlignment = Enum.TextXAlignment.Left
label.TextStrokeColor3 = Color3.fromRGB(0,0,0)
label.TextColor3 = Color3.fromRGB(255,255,255)
label.TextStrokeTransparency = 0
label.Text = instance:GetAttribute("Tooltip") or ""
local TiedTo = Instance.new("ObjectValue", ui)
TiedTo.Name = "TiedTo"
TiedTo.Value = instance
local Connection
local temp_connection
temp_connection = instance:GetAttributeChangedSignal("Tooltip"):Connect(function()
-- Set label text.
label.Text = instance:GetAttribute("Tooltip") or ""
end)
Connection = game:GetService("RunService").RenderStepped:Connect(function()
if ui.Parent == nil then
Connection:Disconnect()
temp_connection:Disconnect()
end
frame.Position = UDim2.new(0, Mouse.X + 16, 0, Mouse.Y-9)
end)
-- Set position at least once before making visible.
frame.Position = UDim2.new(0, Mouse.X + 16, 0, Mouse.Y-9)
ui.Parent = game.Players.LocalPlayer.PlayerGui
return ui
end
-- Storage for tooltip.
local TOOLTIP_STORAGE = nil
-- 0 for hover end event sent, 1 for hover start event sent.
local HOVER_EVENT_STATE = 0
-- Inefficient way of waiting for character to load before starting cycles.
if Player.Character == nil then
Player.CharacterAdded:Wait()
end
-- Wait for render stepping.
game:GetService("RunService").RenderStepped:Connect(function()
-- Perform a raycast.
local OriginPos = workspace.CurrentCamera.CFrame.Position
local Raycast = workspace:Raycast(OriginPos, (Mouse.Hit.Position - OriginPos) * 1.1, RayParams)
if Raycast then
-- Check if we've already casted this before.
-- There is no point in additional processing if this is the case.
if Raycast.Instance ~= HOVER_OBJECT then
if Raycast.Instance:GetAttribute("ClickDetectable") then
-- This is a ClickPoint. Perform max Distance checks.
local Check = math.ceil((Raycast.Instance.Position - Player.Character.HumanoidRootPart.Position).Magnitude)
if Check > Raycast.Instance:GetAttribute("MaxActivationDistance") then
if HOVER_OBJECT ~= nil then
if HOVER_OBJECT:GetAttribute("ClickDetectable") and HOVER_OBJECT:FindFirstChild("OnHover") and HOVER_EVENT_STATE ~= 0 then
HOVER_OBJECT.OnHover:FireServer(false)
HOVER_EVENT_STATE = 0
end
end
HOVERING_OVER_CLICKER = false
HOVER_OBJECT = nil
else
HOVERING_OVER_CLICKER = true
HOVER_OBJECT = Raycast.Instance
if HOVER_OBJECT:GetAttribute("ClickDetectable") and HOVER_OBJECT:FindFirstChild("OnHover") and HOVER_EVENT_STATE ~= 1 then
HOVER_OBJECT.OnHover:FireServer(true)
HOVER_EVENT_STATE = 1
end
end
else
-- Not a clickpoint.
-- Do not perform hover leave if we haven't hovered over anything.
if HOVER_OBJECT ~= nil then
if HOVER_OBJECT:GetAttribute("ClickDetectable") and HOVER_OBJECT:FindFirstChild("OnHover") and HOVER_EVENT_STATE ~= 0 then
HOVER_OBJECT.OnHover:FireServer(false)
HOVER_EVENT_STATE = 0
end
end
HOVERING_OVER_CLICKER = false
HOVER_OBJECT = nil
end
end
else
-- Nothing to ray to.
if HOVER_OBJECT ~= nil then
if HOVER_OBJECT:GetAttribute("ClickDetectable") and HOVER_OBJECT:FindFirstChild("OnHover") and HOVER_EVENT_STATE ~= 0 then
HOVER_OBJECT.OnHover:FireServer(false)
HOVER_EVENT_STATE = 0
end
end
HOVERING_OVER_CLICKER = false
HOVER_OBJECT = nil
end
-- Perform update
if HOVERING_OVER_CLICKER then
Player:GetMouse().Icon = Raycast.Instance:GetAttribute("CursorIcon")
if TOOLTIP_STORAGE ~= nil then
-- Check if tied to matches.
if TOOLTIP_STORAGE.TiedTo.Value ~= HOVER_OBJECT then
TOOLTIP_STORAGE:Destroy()
TOOLTIP_STORAGE = _generateTooltip(HOVER_OBJECT)
end
end
-- Check if instance has Tooltip text set.
-- If it does, we want to use it.
if HOVER_OBJECT:GetAttribute("Tooltip") and TOOLTIP_STORAGE == nil then
-- Display a tooltip.
-- CHECK IF TOOLTIP HAS CHANGED AT ALL.
TOOLTIP_STORAGE = _generateTooltip(HOVER_OBJECT)
end
else
Player:GetMouse().Icon = ""
-- If we are displaying a tooltip, destroy it.
if TOOLTIP_STORAGE ~= nil then
TOOLTIP_STORAGE:Destroy()
TOOLTIP_STORAGE = nil
end
end
end)
Mouse.Button1Down:Connect(function()
if not HOVERING_OVER_CLICKER then
return
end
if HOVER_OBJECT == nil then
return
end
if not HOVER_OBJECT:GetAttribute("ClickDetectable") then
return
end
if not HOVER_OBJECT:FindFirstChild("OnClicked") then
return
end
-- Now perform click fire.
HOVER_OBJECT:FindFirstChild("OnClicked"):FireServer(1, true)
end)
Mouse.Button1Up:Connect(function()
if not HOVERING_OVER_CLICKER then
return
end
if HOVER_OBJECT == nil then
return
end
if not HOVER_OBJECT:GetAttribute("ClickDetectable") then
return
end
if not HOVER_OBJECT:FindFirstChild("OnClicked") then
return
end
-- Now perform click fire.
HOVER_OBJECT:FindFirstChild("OnClicked"):FireServer(1, false)
end)
Mouse.Button2Down:Connect(function()
if not HOVERING_OVER_CLICKER then
return
end
if HOVER_OBJECT == nil then
return
end
if not HOVER_OBJECT:GetAttribute("ClickDetectable") then
return
end
if not HOVER_OBJECT:FindFirstChild("OnClicked") then
return
end
-- Now perform click fire.
HOVER_OBJECT:FindFirstChild("OnClicked"):FireServer(2, true)
end)
Mouse.Button2Up:Connect(function()
if not HOVERING_OVER_CLICKER then
return
end
if HOVER_OBJECT == nil then
return
end
if not HOVER_OBJECT:GetAttribute("ClickDetectable") then
return
end
if not HOVER_OBJECT:FindFirstChild("OnClicked") then
return
end
-- Now perform click fire.
HOVER_OBJECT:FindFirstChild("OnClicked"):FireServer(2, false)
end)
-- Perform refresh of raycast parameters when a new instance is added to the taglist.
CollectionService:GetInstanceAddedSignal(CLICKPOINT_TAG):Connect(function(instance: Instance)
RayParams = _generateRayParams()
end)