I'm trying to make a script which i would put inside trophies around the map and display how many trophies each player has however after a whole afternoon of brainstorming i got nil
01 | script.Parent.Touched:connect( function (hit) |
02 | if hit.Parent ~ = nil then |
03 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
04 | if player ~ = nil then |
05 | local stats = player:WaitForChild( "leaderstats" ) |
06 | local trophies = stats: WaitForChild( "TrophiesFound" ) |
07 | local humanoid = hit.Parent:WaitForChild( "Humanoid" ) |
08 | if humanoid ~ = nil then |
09 | if humanoid.Health ~ = 0 then |
10 | trophies.Value = player.leaderstats.TrophiesFound.Value + 1 |
11 |
12 | end |
13 | end |
14 | end |
15 | end |
16 | end ) |
All i've got is this script that adds +1 on the list of the player who touched it but it adds 5 every time i move near it or walking over it. If you can give me any kind of tip or suggestion your input would be greatly appricated
I won't give you actual code, but I'll let you fill in the blanks.
You can get a player by their character from game:GetService("Players"):GetPlayerFromCharacter
. From there, you can put that player into a table, and later iterate over the table and end the function if they're in it.
Put a Boolean value in StarterGui and have the function change it to true if the player clicks it. Have an if statement check whether or not it is true.
1 |
script.Parent.Touched:connect(function(hit)
01 | if (hit.Parent ~ = nil ) then |
02 |
03 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
04 |
05 | if (player ~ = nil ) then |
06 |
07 | if (player.PlayerGui.ClickedValue.Value = = false ) then --Check if false |
08 |
09 | local stats = player:WaitForChild( "leaderstats" ) |
10 |
11 | local trophies = stats:WaitForChild( "TrophiesFound" ) |
12 |
13 | local humanoid = hit.Parent:WaitForChild( "Humanoid" ) |
14 |
15 | if (humanoid ~ = nil ) then |
end)
Also, you might want to consider cleaning up your code. Space out lines and put loop conditions in parentheses.