script.Parent.Touched:Connect(function(Hit)
local player = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent)
if Hit.Name == 'Brush' then
local backpack = player.Backpack
local char = player.Character
if not char then return end
local hum = char.Humanoid
hum:UnequipTools()
wait()
backpack:FindFirstChild("Brush"):Destroy()
end
end)
May you please explain how to do that? I’m new to scripting so pardon my lack of knowledge.
I am getting an error from my other script:
Workspace.NPCs.NPC.UpperTorso.MoneyScript:8: attempt to index nil with ‘leaderstats’
My other script:
--local tool = game.ServerStorage["Brush"]
local giver = game.Workspace.BrushP
local prp = script.Parent
local customer = prp.Parent
script.Parent.Touched:Connect(function(Hit)
if Hit.Parent.Name == 'Brush' and game.StarterPlayer["Dye"].Value == true then
game.Players:GetPlayerFromCharacter(Hit.Parent.Parent).leaderstats.Money.Value += 25
game.StarterPlayer["Dye"].Value = false
-- tool.Parent = game.ServerStorage
end
end)
This error was occurring because it couldn’t find the “leaderstats” in Player class, since you were trying to parent out of it.
Here, I fixed it:
script.Parent.Touched:Connect(function(Hit)
if Hit.Name == 'Brush' and game.StarterPlayer["Dye"].Value == true then -- a tool named brush
game.Players:GetPlayerFromCharacter(Hit.Parent).leaderstats.Money.Value += 25 -- error was here
game.StarterPlayer["Dye"].Value = false
end
end)
I also re-did it a bit:
local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")
local PLR_NAME = "Brush"
local SHOULD_BE = true
local PART = script["Parent"]
PART["Touched"]:Connect(function(HIT)
local MATCH = HIT["Parent"]["Name"]:match(PLR_NAME)
local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
if MATCH and EQUAL then
local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"])
local MONEY = PLAYER["leaderstats"]["Money"]
MONEY["Value"] += 25
STARTERPLAYER["Dye"]["Value"] = false
end
end)
Oh, my bad, I didn’t know that. Usually, when a touch event is fired, the hit parameter is the “part/item” that touched it, so if you are trying to get a tool named “Brush”, you should not parent out of the Hit parameter for that because parenting out of that will return the player class instead.
Here, try this one instead:
local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")
local PART_NAME = "Brush"
local SHOULD_BE = true
local PART = script["Parent"]
PART["Touched"]:Connect(function(HIT)
local MATCH = HIT["Name"]:match(PART_NAME)
local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
if MATCH and EQUAL then
local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"])
local MONEY = PLAYER["leaderstats"]["Money"]
MONEY["Value"] += 25
STARTERPLAYER["Dye"]["Value"] = false
end
end)
Perhaps I’m mistaken… the first script you’ve provided me had the same error as before but the second one doesn’t increase the Money leaderstats Value but I don’t get an error
“Dye” is a BoolValue within StarterPlayer. The BoolValue is set to true once the clone of the tool is given to the player.
I’m assuming by “Part” you’re talking about the tool?
The original tool is in ServerStorage, then a clone of the tool is given to the player and is placed in the player’s inventory/backpack, then once the player touches the NPC (or the NPC’s UpperTorso), the player is then supposed to be given 25 Money & the clone of the tool is supposed to be removed from the player’s backpack/Inventory.
Turns out you were on the right track earlier.
This logic should work, it checks for the touch event, destroys the tool, and gives the player 25 Money.
Here, try this:
local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")
local PART_NAME = "Brush"
local SHOULD_BE = true
local PART = script["Parent"]
PART["Touched"]:Connect(function(HIT)
local MATCH = HIT["Parent"]["Name"]:match(PART_NAME)
local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
if MATCH and EQUAL then
local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"]["Parent"])
local CHARACTER = PLAYER["Character"]
local LEADERSTATS = PLAYER["leaderstats"]
local BACKPACK = PLAYER["Backpack"]
CHARACTER[PART_NAME]:Destroy()
LEADERSTATS["Money"]["Value"] += 25
STARTERPLAYER["Dye"]["Value"] = false
end
end)
local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")
local PART_NAME = "Brush"
local SHOULD_BE = true
local PART = script["Parent"]
PART["Touched"]:Connect(function(HIT)
local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
local MATCH = HIT["Parent"]["Name"]:match(PART_NAME)
local NPC = script["Parent"]["Parent"]
local TOOL = HIT["Parent"]
if MATCH and EQUAL then
local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"]["Parent"])
local CHARACTER = PLAYER["Character"]
local LEADERSTATS = PLAYER["leaderstats"]
TOOL["Color"]["Transparency"] = 1
script["Sound"]:Play()
CHARACTER[PART_NAME]:Destroy()
LEADERSTATS["Money"]["Value"] += 25
STARTERPLAYER["Dye"]["Value"] = false
task["wait"](2)
NPC:FindFirstChild("Purple")["Handle"]["Transparency"] = 0
NPC:FindFirstChild("White")["Handle"]["Transparency"] = 1
task["wait"](35)
NPC:FindFirstChild("Purple")["Handle"]["Transparency"] = 1
NPC:FindFirstChild("White")["Handle"]["Transparency"] = 0
end
end)
Edit 2: Re-wrote the logic, just so it’s a bit more readable.
local STARTERPLAYER = game:GetService("StarterPlayer")
local DYE = STARTERPLAYER:FindFirstChild("Dye")
local PLAYERS = game:GetService("Players")
local NPC = script["Parent"]["Parent"]
local OBJ:{table} = {
[1] = NPC:FindFirstChild("Purple"),
[2] = NPC:FindFirstChild("White")
}
local NAME = "Brush"
local BOOLEAN = true
function TOUCH(...):nil
local PARAMETER:{table} = {...}
local PLAYER = PARAMETER[1]["Parent"]["Parent"]
local TOOL = PARAMETER[1]["Parent"]
local EQUAL:boolean = rawequal(
BOOLEAN,
DYE["Value"]
)
local MATCH:boolean = string["match"](
NAME,
TOOL["Name"]
)
if MATCH and EQUAL then
PLAYER = PLAYERS:GetPlayerFromCharacter(
PLAYER
)
local CHARACTER = PLAYER["Character"]
local LEADERSTATS = PLAYER["leaderstats"]
TOOL["Color"]["Transparency"] = 1
script["Sound"]:Play()
CHARACTER[NAME]:Destroy()
LEADERSTATS["Money"]["Value"] += 25
DYE["Value"] = false
task["wait"](2)
OBJ[1]["Handle"]["Transparency"] = 0
OBJ[2]["Handle"]["Transparency"] = 1
task["wait"](35)
OBJ[1]["Handle"]["Transparency"] = 1
OBJ[2]["Handle"]["Transparency"] = 0
end
end
local PART = script["Parent"]
PART["Touched"]:Connect(TOUCH)
I think I may have corrected it! Let me know what you think—I was guessing this would work:
local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")
local PART_NAME = "Brush"
local SHOULD_BE = true
local PART = script["Parent"]
PART["Touched"]:Connect(function(HIT)
local MATCH = HIT["Parent"]["Name"]:match(PART_NAME)
local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
if MATCH and EQUAL then
local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"]["Parent"])
local CHARACTER = PLAYER["Character"]
local LEADERSTATS = PLAYER["leaderstats"]
local BACKPACK = PLAYER["Backpack"]
CHARACTER[PART_NAME]:Destroy()
LEADERSTATS["Money"]["Value"] += 25
STARTERPLAYER["Dye"]["Value"] = false
script.Sound:Play()
wait(2)
PART["Parent"]:FindFirstChild("Purple").Handle.Transparency = 0
PART["Parent"]:FindFirstChild("White").Handle.Transparency = 1
wait (35)
PART["Parent"]:FindFirstChild("Purple").Handle.Transparency = 1
PART["Parent"]:FindFirstChild("White").Handle.Transparency = 0
end
end)
If you would like to include that part, you should do that a bit like this because the “Color” instance seems to be inside the tool itself and if you destroyed the tool before, you might have an error.
Wowie! Thank you soooo much for your help! You’re absolutely amazing and I sincerely appreciate your help<3 You put in so much effort and were extremely patient with me I hope to be as awesome as you in the future, I’m gonna follow you!