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

Leaderstats + 1 when Handle touches another Part?

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

✓ Accepted answer

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

3 comments

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.

0

what if the tool is in ServerStorage? Would it still be three .Parents?

0

also, it's still not working

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