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

Leaderstats + 1 when Handle touches another Part?

Asked by 4 years ago
script.Parent.Touched:connect(function(hit)  
    if hit.Name =="REMOVER" then 
        local Player=game.Players:GetPlayerFromCharacter(hit.Parent) 
        Player.leaderstats.Score.Value = Player.leaderstats.Score.Value + 1
    end 
end)

I'm trying to figure out leaderstats rn. I want it so that when the handle of a tool touched the part REMOVER, 1 is added to leaderstats. Therefore, I made this script, and put it in the handle...

Its not working. i tried fixing it, i'm new to lua, and i can't find the problem. What should I do?

1 answer

Log in to vote
0
Answered by
asgm 109
4 years ago

Check your code carefully, mistakes like this will be reduced as you get more experienced in coding.

if hit.Name == "REMOVER" then

In this line, you want to define hit as REMOVER, which in case it is, the script goes on to line 3.

local Player=game.Players:GetPlayerFromCharacter(hit.Parent)

In this line, you must remember that REMOVER is now hit's parameter and thus hit.Parent is not krutik55, but instead the parent of REMOVER.

Solution:

local Player=game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)

Explanation: A tool is a descendant of a player when equipped. (krutik55.Tool.Handle.Script), hence the 3 ".Parent"s

0
Bonus: In a ".Touched" event, any first argument you put in, (in your case,it is "hit",but it can be anything like "touchedPart" or "1zjcasiou") will return the part that touches the Part of Part.Touched. asgm 109 — 4y
0
what if the tool is in ServerStorage? Would it still be three .Parents? krutik55 2 — 4y
0
also, it's still not working krutik55 2 — 4y
Ad

Answer this question