Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i make it where if you hit a tree with your axe then it gives you 1 coin?

Asked by 3 years ago
Edited 3 years ago
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local log = script.Parent
local health = 10
local db = false
log.Touched:connect(function(touched)
    if touched.Name == "Blade" then
            health = health - 1
            plr.leaderstats.Value = plr.leaderstats.Coins.Value + 1
        if health == 0 then
            log.Position = Vector3.new(log.Position) + Vector3.new(-52.751, 50.888, -54.404) 
            print("Tree Has Been Cut Down")
            wait(5)
            log.Position = Vector3.new(log.Position) + Vector3.new(-47.558, 78.031, -98.16) 
            health = health + 10
        else 
            local i = 100
                i = i + 0
        end
      end
end)

the error is "attempt to index nil with leaderstats"

0
Could you tell us where the script is located? The tool? Handle? What? raid6n 2196 — 3y
0
the tree. anyways i like ya cut g Configuator 158 — 3y

3 answers

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

As the script is located in the tree and the tree is located in workspace. So, local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent) is like doing local plr = game.Players:GetPlayerFromCharacter(workspace)

local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local log = script.Parent
local health = 10
local db = false
log.Touched:connect(
    function(touched)
        if touched.Name == "Blade" then
         local plr = game.Players:GetPlayerFromCharacter(touched.Parent.Parent)
         if db == false then
          db = true
            health = health - 1
            plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
            if health == 0 then
                log.Position = Vector3.new(log.Position) + Vector3.new(-52.751, 50.888, -54.404)
                print("Tree Has Been Cut Down")
                wait(5)
                log.Position = Vector3.new(log.Position) + Vector3.new(-47.558, 78.031, -98.16)
                health = health + 10
            else
                local i = 100
                i = i + 0
                end
                wait(3)
                db = false
            end
        end
    end
)

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

The error is saying that whatever script.Parent.Parent is, its not a players character. Make sure that the argument in GetPlayerFromCharacter() is pointing to the players character model and nothing else

Log in to vote
0
Answered by 3 years ago

here is not a official script that will work with you game but is a concept

script.Parent.Touched:Connect(function(hit)
       if hit.Parent ==  "Axe" the
        script.Parent.Value.Value = script.Parent.Value.Value+5
     end
end)

Answer this question