Player.ChildAdded script, to add stats not working
#1
I'm trying to add two IntegerValues to the (only - SP game) player's Stats object, but it doesn't seem to work unless I run the script manually with the right-click menu. I've Enabled it, and tried it under World and Players, yet nothing happens. It yields no errors.

I'd appreciate it if someone could check this and tell me if anything's wrong.

Code:
link(game.Players.ChildAdded, function(p)

if p.Type == "Player" then
   local score = create("IntegerValue")
   score.Name = "Score"

   local hiscore = create("IntegerValue")
   hiscore.Name = "Hi-Score"

   score.Parent = p.Stats
   hiscore.Parent = p.Stats --*nothing* happens after all this. Can't even see values in leaderboard.
end
    
_G.score = function(a) --attempting to use these globals tells me the script tried to reference nil, presumably due to player not being defined.
   if p.Stats.Score then
      local lescore = p.Stats.Score.Value
      local lehiscore = p.Stats["Hi-Score"].Value
      
      lescore = lescore + a
      
      if lescore > lehiscore then
         lehiscore = lescore
      end
   end
end

_G.reset = function()
   if p.Stats.Score then
      p.Stats.Score.Value = 0
   end
end
  
end)
Reply
#2
The thing is, scripts don't run once you finish editing them, or when you select "Enabled". The latter is a bit annoying and should be changed in my opinion. To get it to work, you have to right-click it and select "Run" or create a new instance of the script, either by saving and reloading/publishing and playing the game or cutting and pasting the script object.
Reply
#3
(09-04-2012, 02:27 PM)Qwertygiy Wrote: The thing is, scripts don't run once you finish editing them, or when you select "Enabled". The latter is a bit annoying and should be changed in my opinion. To get it to work, you have to right-click it and select "Run" or create a new instance of the script, either by saving and reloading/publishing and playing the game or cutting and pasting the script object.
So, it should work if I load the place again. I've tried that before posting this, but nothing happened. :/ Gotta check again now.
Reply
#4
Just to make sure, are you adding the player after running the script?

And when you load a game in the editor, scripts won't run until you click the play button.
Reply
#5
(09-05-2012, 11:32 PM)Jacob_ Wrote: Just to make sure, are you adding the player after running the script?

And when you load a game in the editor, scripts won't run until you click the play button.
This was exactly the problem - I was used to adding the player before starting the physics. Tongue Now everything's working fine, thank you c:
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)