#sync gui with tower upgrade

280 messages · Page 1 of 1 (latest)

nocturne monolith
#

ye

#

@trim hull

mellow plumeBOT
#

studio** You are now Level 6! **studio

nocturne monolith
#
function upgradeTower(tower, rangePart, hrp, clicked)
    local towerName = tower.Name
    local upgradeAmount = tower.Upgrade.Value
    local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount).Frame
    local moneyAmount = upgradeGui.UpgradeAmount.Value
    local upgradeButton = upgradeGui.Upgrade
    
    upgradeGui.Visible = true
    
    upgradeButton.MouseButton1Click:Connect(function()
        local playerMoney = player.leaderstats.Money.Value
        if playerMoney >= moneyAmount then
            upgradeGui.Visible = false
            local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount, moneyAmount)
            upgradeTower(upgradedTower)
        end
    end)    

    if rangePart.Parent == hrp and clicked then
        upgradeGui.Visible = true
    elseif rangePart.Parent ~= hrp and clicked then
        upgradeGui.Visible = true
    elseif not clicked then
        upgradeGui.Visible = false
    end
end```
arctic mural
#

i dont think you can store .value as a variable

#

store the object and do .value later

nocturne monolith
arctic mural
#

wait

#

dont do local upgradeAmount = tower.Upgrade.Value

#

do local upgradeAmount = tower.Upgrade

nocturne monolith
#

and why is it setting my money to -7775 instead of subtracting 100 lol

#

alr

arctic mural
#

wait i didnt actually read the script

#

let me check it

nocturne monolith
arctic mural
#

idk but i had a similar issue before

#

like you cant store .value as a variable

#

it makes no sense

#

but its like that

nocturne monolith
#

alr, but it didnt improve much

arctic mural
#

sure let me see

#

can u only send the part with the issue

#

like the line that gives the error

nocturne monolith
#

yikes whats wrong with the indentation

#

1 sec

arctic mural
#

rangepart doesnt exist?

nocturne monolith
#

oh yeah ur right

#

oh ik why

#

nevermind idk why

arctic mural
#

sorry i need to leave

nocturne monolith
#

np

#

but heres the whole script

#

wait

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local upgradeTowerEvent = events:WaitForChild("UpgradeTower")
local towerFolder = workspace:WaitForChild("Towers")
local players = game:GetService("Players")
local player = players.LocalPlayer
local rangePart = workspace.Utility:WaitForChild("rangePart")

towerFolder.ChildAdded:Connect(function(tower)
    local clicked = false;
    local hitbox = tower:WaitForChild("Hitbox")
    local clickDetector = hitbox.ClickDetector
    local hrp = hitbox.Parent.HumanoidRootPart
    local radius = hrp.Parent.Range.Value
    local debounce = false;
    
    clickDetector.MouseClick:Connect(function()
        if debounce then return end
        debounce = true
        
        if rangePart.Parent == hrp and clicked then
            rangePart.Transparency = 1
            clicked = false
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
            return
        elseif rangePart.Parent ~= hrp and clicked then
            clicked = false
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
        end
        
        if not clicked then
            rangePart.Parent = hrp
            rangePart.Size = Vector3.new(1, radius * 2, radius * 2)
            rangePart.CFrame = (hrp.CFrame * CFrame.new(0, -hrp.Parent.Humanoid.HipHeight - 1, 0)) * CFrame.Angles(0, 0, math.rad(90))
            rangePart.Transparency = 0.8
            clicked = true
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
            return
        end
    end)
end)```
#
function upgradeTower(tower, rangePart, hrp, clicked)
    local towerName = tower.Name
    local upgradeAmount = tower.Upgrade.Value
    local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount).Frame
    local moneyAmount = upgradeGui.UpgradeAmount.Value
    local upgradeButton = upgradeGui.Upgrade
    
    upgradeGui.Visible = true
    
    upgradeButton.MouseButton1Click:Connect(function()
        local playerMoney = player.leaderstats.Money.Value
        if playerMoney >= moneyAmount then
            upgradeGui.Visible = false
            local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount, moneyAmount)
            upgradeTower(upgradedTower, rangePart, hrp, clicked)
        end
    end)    

    if rangePart.Parent == hrp and clicked then
        upgradeGui.Visible = true
    elseif rangePart.Parent ~= hrp and clicked then
        upgradeGui.Visible = true
    elseif not clicked then
        upgradeGui.Visible = false
    end
end```
#

here

#
upgradeTowerEvent.OnServerInvoke = function(player, tower, upgradeAmount, moneyAmount)
    tower.Upgrade.Value += 1
    local playerMoney = player.leaderstats.Money
    playerMoney.Value -= moneyAmount

    local indexTower = towerDatabase[tower.Name .. upgradeAmount + 1]

    tower.Damage.Value = indexTower.Damage -- error here
    tower.Range.Value = indexTower.Range
    tower.Firerate.Value = indexTower.Firerate

    return tower
end```
#

and its triggering multiple times when i click the upgrade button

slim ridge
#

Someone look on studio help classic

nocturne monolith
#

@trim hull

trim hull
# nocturne monolith i get what you mean but what difference does it make
local level = towerLevel.Value --towerLevel.Value returns the value of the container, it doesnt represent the property itself. so typeof(level) would be an int (or number, wtvr object it is)

level = 10 --this changes the variable named "level", not the towerLevel object itself


local towerLev = towerLevel --sets the towerLev variable to the object *instance*

towerLev.Value = 10 --sets the property named 'value' of towerLevel object to 10
#

thats the difference, so it wouldnt update properly

nocturne monolith
#

im indexing something that doesnt exist yet

#

do i just use a debounce

#

so it doesnt trigger many times

trim hull
#

what is IndexTower?

#

and when are elements being added

nocturne monolith
#

what elements

nocturne monolith
#

wait ill open roblox studio in a sec

trim hull
#

no

#

the debounce doesnt really do much here

#

if IndexTower is nil, then there is no element in that array

#

with that index

nocturne monolith
#

i know

trim hull
#

so how are elements being added to that array

nocturne monolith
#

uh im adding them manually

#
local towerDatabase = {
    ["Vandal0"] = {
        ["Damage"] = 25,
        ["Firerate"] = 1,
        ["Range"] = 20
    },
    ["Vandal1"] = {
        ["Damage"] = 40,
        ["Firerate"] = 0.8,
        ["Range"] = 25
    }
}```
nocturne monolith
#

so its triggering multiple times

#

for some reason :(

trim hull
nocturne monolith
#

yea

#

and my moneys going like -2000

#

not subratcting 100 but way more

nocturne monolith
#

i can fix it then

#

indextower being nil

trim hull
#

theres no builtin method

#

youll have to brute force it

#

im pretty sure

#

can you try printing the player's money and the cost amount right before upgrading

#

then the same thing in the function that actually does it

nocturne monolith
#
print(playerMoney, costAmount)```
nocturne monolith
#

you mean doing the math on the client then sending it

trim hull
#

no you should do the math on server

#

i meant print the values on server as well

#

but it shouldnt really matter

#

is the 0 print relevant?

nocturne monolith
#

ye lua local count = 0 for i, v in pairs(towerDatabase) do if i == tower.Name then count += 1 end end print(count) i did this

#

but it cant pass the comparison for some reason

trim hull
#

whats that for?

#

oh to count the keys?

nocturne monolith
#

ye ye

trim hull
#

a recursive call would be better

#

and this only counts its first scope elements

#

so array inside array wouldnt be counted

nocturne monolith
#

yep i dont need it to be counted

nocturne monolith
trim hull
#

oh nevermind then

trim hull
#

in that case you can do #array

trim hull
#

but keep the other prints as well

nocturne monolith
nocturne monolith
#

:)

trim hull
#
local tabl = {1,2,3,4,5}
local val = #tabl

print(val)
blazing muralBOT
#
Program Output
5

trim hull
#
local tabl = {
  ['1'] = 'a',
  ['10'] = 'b'
}
local val = #tabl

print(val)
blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['

nocturne monolith
#

oof

mellow plumeBOT
#

studio** You are now Level 7! **studio

blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['

trim hull
#

what

#

oh

nocturne monolith
#

u gotta put it in quotes or something

blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:2: '}' expected (to close '{' at line 1) near '='

trim hull
#

weird

trim hull
#

anyways idk whats going on 💀 but it counts the max index not the length

nocturne monolith
#
local tabl = {
  ["1"] = "a"
  ["10"] = "b"
}
local val = #tabl

print(val)```
blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:3: '}' expected (to close '{' at line 1) near '['

nocturne monolith
#

what (

trim hull
#

oh forgot the comma 💀

nocturne monolith
#

😎

blazing muralBOT
#
Program Output
1

trim hull
#

there

#

😎

#

string values wont do that i think

nocturne monolith
#
local towerDatabase = {
    ["Vandal0"] = {
        ["Damage"] = 25,
        ["Firerate"] = 1,
        ["Range"] = 20
    },
    ["Vandal1"] = {
        ["Damage"] = 40,
        ["Firerate"] = 0.8,
        ["Range"] = 25
    }
}

local val = #towerDatabase
print(val)
blazing muralBOT
#
Program Output
0

blazing muralBOT
#
Program Output
0

trim hull
#

😭

nocturne monolith
#

yea how do i count the keys then

trim hull
#

i was mistekan evilcat

trim hull
#

anyways back to our problem

nocturne monolith
#
local towerDatabase = {
    ["Vandal0"] = {
        ["Damage"] = 25,
        ["Firerate"] = 1,
        ["Range"] = 20
    },
    ["Vandal1"] = {
        ["Damage"] = 40,
        ["Firerate"] = 0.8,
        ["Range"] = 25
    }
}

local count = 0
local tower = "Vandal"
    for i, v in pairs(towerDatabase) do
        if i == tower then
            count += 1
        end
    end
    print(count)```
trim hull
blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:17: syntax error near '+'

blazing muralBOT
#
Program Output
/opt/wandbox/lua-5.4.3/bin/lua: prog.lua:18: syntax error near '+'

nocturne monolith
#

ugh

nocturne monolith
trim hull
#

oh thats what you meant by add a debounce

#

but it would be a different connection so it wouldnt be firing twice

nocturne monolith
#

nah u still gotta click on the button

#

except its not a clickdetector

#

so idk

#
local count = 0
    for i, v in pairs(towerDatabase) do
        if i == tower.Name then
            count += 1
        end
    end``` but why isnt this working
#

it cant pass the if check

#

even tho the values are the same

trim hull
#

try in studio

#

compiler dumb

nocturne monolith
#

i am

#

wait

#
local count = 0
    for i, v in pairs(towerDatabase) do
        if i == tower.Name then
            count += 1
        end
    end
    print(count)```
```lua
local towerDatabase = {
    ["Vandal0"] = {
        ["Damage"] = 25,
        ["Firerate"] = 1,
        ["Range"] = 20
    },
    ["Vandal1"] = {
        ["Damage"] = 40,
        ["Firerate"] = 0.8,
        ["Range"] = 25
    }
}```
trim hull
#

print i and v every iteration

nocturne monolith
#

oh woops

#

i need to add the number to the end

#

🤦‍♂️

trim hull
#

?

nocturne monolith
#

tower.Name is Vandal but in the database its Vandal0 or Vandal1

trim hull
#

oh alr

nocturne monolith
#

ye so how do i add a number at the end

trim hull
#

'Vandal'..i

#

use ipairs

#

oh wait

#

yeha

#

actually what are u doing?

#

i dont understand

#

but use .. to concatanate

nocturne monolith
#

i gotta remove the number at the end instead

trim hull
#

thats more annoying

#

something with string.sub?

#

i didnt bother with string methods 💀

nocturne monolith
#

alright fixed it

#
upgradeTowerEvent.OnServerInvoke = function(player, tower, upgradeAmount, costAmount)
    tower.Upgrade.Value += 1
    local playerMoney = player.leaderstats.Money
    playerMoney.Value -= costAmount

    local towerUpgradeLimit = 0
    for i, v in pairs(towerDatabase) do
        local dbTowerName = i:gsub(".?$","")
        
        if dbTowerName == tower.Name then
            towerUpgradeLimit += 1
        end
    end
    
    if upgradeAmount >= towerUpgradeLimit then
     local indexTower = towerDatabase[tower.Name .. upgradeAmount + 1]

     tower.Damage.Value = indexTower.Damage
     tower.Range.Value = indexTower.Range
     tower.Firerate.Value = indexTower.Firerate    
    end
    
    return tower
end``` this is working
#

how do i add a debounce?

#
upgradeButton.MouseButton1Click:Connect(function()
        local playerMoney = player.leaderstats.Money
        if playerMoney.Value >= costAmount.Value then
            upgradeGui.Visible = false
            local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
            upgradeTower(upgradedTower, rangePart, hrp, clicked)
            return
        end
    end)    

    if rangePart.Parent == hrp and clicked then
        upgradeGui.Visible = true
    elseif rangePart.Parent ~= hrp and clicked then
        upgradeGui.Visible = true
    elseif not clicked then
        upgradeGui.Visible = false
    end``` here
#

the click is triggering multiple times

slim ridge
#

Does anybody know how to click the character and it will let me buy the character and put it in my inventory

mellow plumeBOT
#

studio** You are now Level 2! **studio

slim ridge
#

Like i click on this dummy and it let's me buy the WHOLE character and it goes into my inventory

#

Does anybody know how to do that?

nocturne monolith
#

ye

#

ofc

vestal oar
slim ridge
#

How?

#

What do I press?

vestal oar
slim ridge
#

Is there a kit for that then?

vestal oar
#

watch a tutorial or smthn

nocturne monolith
slim ridge
vestal oar
#

.

nocturne monolith
#

idk

slim ridge
#

No

#

Listen guys

#

How do you buy a dummy and it adds to ur inventory

#

Let me find an example

#

On tiktok

nocturne monolith
#

you mean like add it to an inventory youve created?

nocturne monolith
vestal oar
#
local debounce = false
upgradeButton.MouseButtonClick:Connect(function()
 if debounce then return end
 debounce = true
 -- script 
 task.wait(2)
 debounce = false
end)
nocturne monolith
#
function upgradeTower(tower, rangePart, hrp, clicked)
    local towerName = tower.Name
    local upgradeAmount = tower.Upgrade
    local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount.Value).Frame
    local costAmount = upgradeGui.UpgradeAmount
    local upgradeButton = upgradeGui.Upgrade
    local debounce = false
    upgradeGui.Visible = true
    
    upgradeButton.MouseButton1Click:Connect(function()
        if debounce then return end
        debounce = true
        
        local playerMoney = player.leaderstats.Money
        if playerMoney.Value >= costAmount.Value then
            upgradeGui.Visible = false
            local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
            upgradeTower(upgradedTower, rangePart, hrp, clicked)
        end
        
        task.wait(1)
        
        debounce = false
    end)    

    if rangePart.Parent == hrp and clicked then
        upgradeGui.Visible = true
    elseif rangePart.Parent ~= hrp and clicked then
        upgradeGui.Visible = true
    elseif not clicked then
        upgradeGui.Visible = false
    end
end``` you mean like this
#

but it still rarely triggers multiple times

slim ridge
#

Finally found it

#

Sorry it took a while

#

Like this

#

For ppl to buy my dummy

#

And it's in their inventory

#

And then u find it here

vestal oar
#

make it print smthn when someone clicks the button to check

nocturne monolith
#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local upgradeTowerEvent = events:WaitForChild("UpgradeTower")
local towerFolder = workspace:WaitForChild("Towers")
local players = game:GetService("Players")
local player = players.LocalPlayer
local rangePart = workspace.Utility:WaitForChild("rangePart")

towerFolder.ChildAdded:Connect(function(tower)
    local clicked = false;
    local hitbox = tower:WaitForChild("Hitbox")
    local clickDetector = hitbox.ClickDetector
    local hrp = hitbox.Parent.HumanoidRootPart
    local radius = hrp.Parent.Range.Value
    local debounce = false;
    
    clickDetector.MouseClick:Connect(function()
        if debounce then return end
        debounce = true
        
        if rangePart.Parent == hrp and clicked then
            rangePart.Transparency = 1
            clicked = false
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
            return
        elseif rangePart.Parent ~= hrp and clicked then
            clicked = false
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
        end
        
        if not clicked then
            rangePart.Parent = hrp
            rangePart.Size = Vector3.new(1, radius * 2, radius * 2)
            rangePart.CFrame = (hrp.CFrame * CFrame.new(0, -hrp.Parent.Humanoid.HipHeight - 1, 0)) * CFrame.Angles(0, 0, math.rad(90))
            rangePart.Transparency = 0.8
            clicked = true
            debounce = false
            upgradeTower(tower, rangePart, hrp, clicked)
            return
        end
    end)
end)
#
function upgradeTower(tower, rangePart, hrp, clicked)
    local towerName = tower.Name
    local upgradeAmount = tower.Upgrade
    local upgradeGui = player.PlayerGui.TowerUpgrades:WaitForChild(towerName .. upgradeAmount.Value).Frame
    local costAmount = upgradeGui.UpgradeAmount
    local upgradeButton = upgradeGui.Upgrade
    local debounce = false
    upgradeGui.Visible = true
    
    upgradeButton.MouseButton1Click:Connect(function()
        if debounce then return end
        
        debounce = true
        local playerMoney = player.leaderstats.Money
        if playerMoney.Value >= costAmount.Value then
            upgradeGui.Visible = false
            local upgradedTower = upgradeTowerEvent:InvokeServer(tower, upgradeAmount.Value, costAmount.Value)
            print("clicked")
            upgradeTower(upgradedTower, rangePart, hrp, clicked)
            task.wait(1)
            debounce = false
            return
        end
        
        task.wait(1)
        debounce = false
        return
    end)    

    if rangePart.Parent == hrp and clicked then
        upgradeGui.Visible = true
    elseif rangePart.Parent ~= hrp and clicked then
        upgradeGui.Visible = true
    elseif not clicked then
        upgradeGui.Visible = false
    end
end```
nocturne monolith
#

clicked once lol

#

@vestal oar

trim hull
#

I don’t think it’s being clicked multiple times, unless you spam it

#

The problem is that one click invokes the server multiple times, resulting in multiple money subtractions right?

#

I don’t think it’s possible for the mb1 event to fire like that because it’s mb1 down and up

#

Basically, it can only fire once for every click

nocturne monolith
trim hull
slim ridge
trim hull
#

no 💀

slim ridge
#

Can u not read the chat

#

I literally said it

trim hull
#

I didn’t want to read

slim ridge
#

Then don't say shit then

trim hull
#

lmao

slim ridge
#

😂😂

trim hull
#

Anyways

#

That’s not what I was talking about

#

If u have a problem u can make your own post

#

U almost crashed my phone cuz I had to scroll so much 🙄🙄🙄

slim ridge
trim hull
#

Happens a lot

slim ridge
#

Yh

trim hull
#

If u repost some people are bound to see it

#

It’s just kinda rude to come in to other posts

slim ridge
#

Nah

#

I give up

slim ridge
#

I js came here bcz no one answerd my posts and since some ppl were typing here I js said it here

trim hull
#

I actually can’t think of a valid reason lmao but idk it just feels rude

slim ridge
#

Yh for u

trim hull
#

ok

#

Well anyways

#

Now that it’s fixed, make a post next time

slim ridge
#

Girlie pop

mellow plumeBOT
#

studio** You are now Level 3! **studio

slim ridge
#

Wdym fixed

#

Nothing is fixed

#

I js change my damn mind