So this is what I have revised it to and now it’s working if the player is touching the button, but it only works while the player is moving around. There’s still a problem with the player standing still and not receiving any stats which would probably be fixed with a Region3.
local buttons = game.Workspace.Buttons.MultiplierButtons
local debounce = false
--[[Button variables]]
local button1 = buttons.Button1
local function createButton(player, button)
if player then
if player.leaderstats.Cash.Value >= button.Config.Price.Value then
debounce = true
while debounce == true do
player.leaderstats.Cash.Value -= button.Config.Price.Value -- subtracts cash from player
player.multiplier.Value += button.Config.MultiToAdd.Value * (1 + player.rebirths.Value) -- adds multiplier
task.wait(1)
end
else
print("person is broke, sorry")
end
end
end
button1.Button.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
createButton(player, button1)
end)
button1.Button.TouchEnded:Connect(function(hit)
debounce = false
end)
I tried using a Region3 but have never used it before. Hopefully you can point me in the right direction. I was looking at this post to try to figure it out: :GetPartBoundsInBox Help
Here’s my current attempt:
local buttons = game.Workspace.Buttons.MultiplierButtons
local debounce = false
--[[Button variables]]
local button1 = buttons.Button1
local filterObjects = {button1.Button, game.Workspace.Baseplate}
local boxPosition = button1.Button.CFrame
local boxSize = button1.Button.Size + Vector3.new(0,1,0)
local maxObjectsAllowed = 10
local params = OverlapParams.new(filterObjects,Enum.RaycastFilterType.Blacklist,maxObjectsAllowed,"Default")
local objectsInSpace = workspace:GetPartBoundsInBox(boxPosition,boxSize,params)
local function createButton(player, button)
for i, v in pairs(objectsInSpace) do
if v.Parent:FindFirstChild("Humanoid") then
if player.leaderstats.Cash.Value >= button.Config.Price.Value then
debounce = true
while debounce == true do
player.leaderstats.Cash.Value -= button.Config.Price.Value -- subtracts cash from player
player.multiplier.Value += button.Config.MultiToAdd.Value * (1 + player.rebirths.Value) -- adds multiplier
task.wait(1)
end
else
print("person is broke, sorry")
end
end
end
end
button1.Button.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
createButton(player, button1)
end)
button1.Button.TouchEnded:Connect(function(hit)
debounce = false
end)
I realize that it fails because local objectsInSpace
is only defined once the game runs, but I’m not sure how best to solve that. I tried putting in a while loop but it didn’t seem to really work. I have also tried defining it inside of the createButton function like so
local function createButton(player, button)
local objectsInSpace = workspace:GetPartBoundsInBox(boxPosition,boxSize,params)
and it actually does work, but only if the player is moving.