I have a script in StarterPlayerScripts to handle E to interact events, which is meant to check if a part is within range, enable a BillboardGui, then when the E Key is pressed, TweenSize a grey label from YScaleSize 1 to 0, exposing a white label underneath, yes a little (read a lot) like Mad City.
There are 2 issues with this:
- I am trying to use the callback function to trigger giving cash to the player once the tween has completed. However, this triggers as soon as the players presses the E key.
- I have a table of parts which can be interacted with, ATM1, ATM2, ATM3. If only ATM1 is in the table, then I can approach it, press the E key and it does it’s thing (ignoring issue 1 above for now).
If I then add ATM2 into the table, ATM1 stops responding to the E key, but ATM2 is OK. Add ATM3, then ATM1 & ATM2 fail to respond.
There is obviously something very wrong with how I am understanding the function callback within the tween and also with how I am handling the table of objects to interact with.
I started to read about collectionservice as a better way to handle the interactions as more are added, but I am not sure how to go about this.
-- SERVICES
local tweenservice = game:GetService("TweenService")
-- VARIABLES
local debounce = false
local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil
local char = player.Character
local interactDistance = 10
local canInteract = false
local interactables = {
workspace.ATM1.Screen,
workspace.ATM2.Screen,
workspace.ATM3.Screen
}
local nearestpart = nil
local resetTimer = 10
local function giveCash(part)
print("Give ", char.Name, " CASH from ", part.Parent.Name)
if part.Name == "Screen" then
wait(5)
part.BillboardGui.Enabled = false
part.BillboardGui.Frame.lbl_grey.Size = UDim2.new(1, 0, 1, 0)
part.Color = Color3.new(1, 0, 0)
nearestpart = nil
wait(resetTimer)
debounce = false
part.BillboardGui.Enabled = true
part.BillboardGui.Frame.lbl_grey.Size = UDim2.new(1, 0, 1, 0)
part.Color = Color3.new(0.5, 0.73, 0.85)
end
end
game:GetService("UserInputService").InputBegan:connect(function(key)
if key.KeyCode == Enum.KeyCode.E and canInteract == true then
if debounce == true then
return
else
debounce = true
print(player, " pressed E")
local interactpart = nearestpart
if nearestpart.Name == "Screen" then
local Etween = interactpart.BillboardGui.Frame.lbl_grey:TweenSize(
UDim2.new(1, 0, 0, 0), -- endSize (required)
Enum.EasingDirection.In, -- easingDirection (default Out)
Enum.EasingStyle.Sine, -- easingStyle (default Quad)
5, -- time (default: 1)
true, -- should this tween override ones in-progress? (default: false)
giveCash(nearestpart) -- a function to call when the tween completes (default: nil)
)
end
wait(5)
debounce = false
end
end
end)
-- Start E Bindable Events
while wait(0.5) do
for i, part in pairs(interactables) do
if (part.Position - char.HumanoidRootPart.Position).magnitude < interactDistance then
canInteract = true
nearestpart = part
else
canInteract = false
nearestpart = nil
end
end
end
Any pointers would be greatly appreciated.
I spend a lot of time reading the Q&As on the DevForum and am truly astounded at how helpful the community is. I just paddle around the edges looking for hints as to how to implement my sons latest idea into his game.
The game itself is Downfall Avenue and the ATMs are outside of the bank, goading me, daring me to fix them…