Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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"

2 comments

0

Could you tell us where the script is located? The tool? Handle? What?

0

the tree. anyways i like ya cut g

3 answers

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
)

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.