#help with changing one objects int value (hits required to chop)

1 messages · Page 1 of 1 (latest)

earnest cove
#

The goal is when activating a prox prompt, (and a leaderstat is above or equal to) a certain value, it will make the axe take less hits to cut down trees. The actual "hit" int value is in the tree.

#

the "hithandler" script


script.Parent.Hit.OnServerEvent:Connect(function()
    if script.Parent.Hits.Value == 1 then
        script.Parent.Dead.Value = true
        if script.Parent.Trunk:FindFirstChild("HealthGui") then
            local ui = script.Parent.Trunk:FindFirstChild("HealthGui")
            ui.WhiteBar:Destroy()
        end
        for i,v in script.Parent:GetChildren() do
            if v.Name == "Leaves" then
                v:Destroy()
            end
        end
        if script.Parent.Trunk:FindFirstChild("ProximityPrompt") then
            script.Parent.Trunk.ProximityPrompt.Enabled = true
            script.Parent.Trunk.Anchored = false
        end
    else
        script.Parent.Hits.Value -= 1
        if script.Parent.Trunk:FindFirstChild("HealthGui") then
            local ui = script.Parent.Trunk:FindFirstChild("HealthGui")
            ui.WhiteBar.Size = UDim2.new(script.Parent.Hits.Value/initialHits, 0, 0.2, 0)
        end
    end
end)```
#

the actual swing handler in the axe itself


script.Parent.Activated:Connect(function()
    if deb == false then
        deb = true
        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
        if mouse.Target.Name == "Trunk" then
            local Distance = (mouse.Target.Position - player.Character.HumanoidRootPart.Position).Magnitude
            if Distance <= 10 then
                if mouse.Target.Parent:FindFirstChild("Dead") then
                    if mouse.Target.Parent:FindFirstChild("Dead").Value == true then
                        
                    else
                        if mouse.Target.Parent:FindFirstChild("Hit") then
                            mouse.Target.Parent:FindFirstChild("Hit"):FireServer()
                        end
                    end
                end
            end
        end
        wait(.8)
        deb = false
    end
end)```
#

the problem comes from that the actual "hits" isnt inside the axe its inside the tree

urban summit
#

damn

#

I dont have yet solution to your problem, but I can see that your code will be very hard to maintain and it uses wait instead of task.wait

#

you should organize it more

#

also nesting gives its effect too

earnest cove
#

i used a youtube video for it and the person didnt really explain it alot, i thought i could learn from the script itself but it all seems very vague.

#

i hoped to build up from it but as u can see its not going well

urban summit
#

I mean probably video was atleast 3-4 years old

#

Because wait is already red flag

#

Like I can see how logic works

#

Its alright I guess, I mean for youtube video its logic if it works it works

#

But lioe for learning thats awful

#

Like

urban summit
#

I didnt understand goal really

earnest cove
#

i want it so you can upgrade your axe after getting lets say 5 wood so it takes like 10 hits or a multi like you say

obsidian stone
#

why not just give the tree hp

#

and the axe itself a damage attribute

earnest cove
urban summit
#

Like in your current structure I need to think a bit how to not make it worse

#

Because its already so bad

obsidian stone
urban summit
#

I know that judging in terms of pro coding this is awful, but if you are beginner thats alright

earnest cove
#

because theres other code not mentioned too and its all just

urban summit
#

I suggest you to remake it yourself

#

Without trying to use tutorial, like use own logic

#

Even if it would be awful, just try using recent knowledge

#

Because clearly this 4 years old tutorial is so bad currently

#

It teaches you awful approaches like nesting, using deprecated methods

#

lack of local variables

#

violating single responsibility

earnest cove
#

ill try and explain what i know


script.Parent.Hit.OnServerEvent:Connect(function()  /// dont really understand
    if script.Parent.Hits.Value == 1 then
        script.Parent.Dead.Value = true
        if script.Parent.Trunk:FindFirstChild("HealthGui") then
            local ui = script.Parent.Trunk:FindFirstChild("HealthGui")
            ui.WhiteBar:Destroy()
        end
        for i,v in script.Parent:GetChildren() do
            if v.Name == "Leaves" then
                v:Destroy() /// gets rid of the leaves on the tree
            end
        end
        if script.Parent.Trunk:FindFirstChild("ProximityPrompt") then /// finds prox prompt
            script.Parent.Trunk.ProximityPrompt.Enabled = true /// turns on prox prompt
            script.Parent.Trunk.Anchored = false  /// unanchors tree trunk
        end
    else
        script.Parent.Hits.Value -= 1
        if script.Parent.Trunk:FindFirstChild("HealthGui") then
            local ui = script.Parent.Trunk:FindFirstChild("HealthGui")
            ui.WhiteBar.Size = UDim2.new(script.Parent.Hits.Value/initialHits, 0, 0.2, 0) /// divides the white bar evenly so its smooth no matter the hit value.
        end
    end
end)```
urban summit
#

Like I can understand you are at tutorial stage and currently dont understand at all how to maintain scripts

#

Learn more about stuff you dont understand

#

Like this first commented line

#

script.Hit.OnServerEvent

#

like thats remote event connected function

#

once remote fires it calls function which is this

#

like your tool activates and it fires event

#

Learn about that

#

Just dont get stuck on things like that lol

earnest cove
#

whats the dev thing called where it gives guides on scripting

#

nvm its documentation

hardy hemlock
#

BrawlDev better

earnest cove
#

i wanna enjoy coding but it feels like im just always gonna be stuck at like the bottom

obsidian stone
#

just do something everyday

#

just get better 1% each day

#

and after years you will be so much better