Yes there is another way to do it. It is how you're defining your player.
local canTouch = true
local function onTouched(hit)
if canTouch then
canTouch = false
local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
local character = humanoid.Parent
local player = players:GetPlayerFromCharacter(character)
if player then
player.leaderstats.Coins.Value = 0
end
end
wait(0.3)
canTouch = true
end
end
Now you may be asking "Well how exactly does this work?"
First we define the humanoid; we use ternary operators in our favor. In a statement such as a = b or d it will set a to which variable has truthiness, if both have truthiness then it is set to d
(If you do not know what truthiness is I suggest looking at this article Truthy, and this Expressions Tutorial)
Now we check if the humanoid actually exists :FindFirstChild() returns the instance or nil if it does not exist, so if humanoid exists then continue code.
Now we know that the parent of a humanoid will be the player's (or NPC's) physical character model. Having reference to their character allows us to use the :GetPlayerFromCharacter() method which returns the player that owns that character model. This can be nil and it will only be nil if the player is not found. We use :GetPlayerFromCharacter() and not players[character.Name] because the humanoid that touched the brick might be a NPC in the game, which have no player instance in the players service.
Finally if the player exists, then index their leaderstats, then their coins, and set the value to 0
2 comments
that's pointless
lmao