1 | function addxp(hit) |
2 | local dude = hit.Parent:WaitForChild( 'Character' ) |
3 | dude.xp.Value = 1 |
4 | script.Parent:Destroy() |
5 | end |
6 |
7 | script.Parent.Touched:connect(addxp) -- the script's parent is a brick |
Im 90% sure this should work, but when i touch the brick absolutely nothing happens. I checked the values after too, it remains at 0. Anything I did wrong?
If you are trying to access the player's Character
, check if it has a Humanoid
.
1 | function addxp(hit) |
2 | local dude = hit.Parent:WaitForChild( "Humanoid" ).Parent |
3 | dude.xp.Value = 1 |
4 | script.Parent:Destroy() |
5 | end |
6 |
7 | script.Parent.Touched:connect(addxp) |
or you can use the GetPlayerFromCharacter
event.
1 | function addxp(hit) |
2 | local dude = game.Players:GetPlayerFromCharacter(hit.Parent).Character -- accessing the player through the character |
3 | dude.xp.Value = 1 |
4 | script.Parent:Destroy() |
5 | end |
6 |
7 | script.Parent.Touched:connect(addxp) |
You're leaving out some needed information. Is "xp" something in the character or in the leaderboards?
Anyway, I'll do both.
In character:
1 | script.Parent.Touched:connect( function (hit) |
2 | local dude = hit.Parent:WaitForChild( "Humanoid" ).Parent --Wait for the humanoid, checking if "hit" is a character |
3 | local xp = dude:WaitForChild( "xp" ) |
4 | xp.Value = 1 |
5 | end ) |
In player:
1 | script.Parent.Touched:connect( function (hit) |
2 | local dude = hit.Parent:WaitForChild( "Humanoid" ).Parent |
3 | local player = game.Players:GetPlayerFromCharacter(dude) |
4 | local leaderstats = player:WaitForChild( "leaderstats" ) |
5 | local xp = leaderstats:WaitForChild( "xp" ) |
6 | xp.Value = 1 |
7 | end ) |
Hope this helps! :)
There cannot be found thing such as Character. Character can be found from game.Players.Player.Character. You should also add the xp To Leaderboards, check out how to in wiki if you don't now: http://wiki.roblox.com/index.php?title=Leaderboards
01 | function getxp(hit) |
02 | for i,Player in ipairs (game.Players:GetPlayers()) do |
03 | if hit.Parent = Player.Character then |
04 | local Character = hit.Parent |
05 | local Player = game.Players:GetPlayerFromCharacter(Character) |
06 | Character.xp.Value = Character.xp.Value + 1 --I made this like this, you can do like you want |
07 | script.Parent:Destroy() |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:connect(getxp) |