Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Works in Studio but not in game?

Asked by 8 years ago

I'm trying to make it to were every 10 seconds you get 5 credits. works in studio but not in game... Everything works fine but the adding cash at the bottom of the script. Any idea on how to make it work?

function CreateStats(NewPlayer)
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Value = 0
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Value = 50
    Credits.Parent = leaderstats
    leaderstats.Parent = NewPlayer
    return leaderstats
end
game.Players.ChildAdded:connect(CreateStats)
while true do
  wait(10)
  game.Players.LocalPlayer.leaderstats.Credits.Value = game.Players.LocalPlayer.leaderstats.Credits.Value + 5
end
0
LocalPlayer can only be used in a LocalScript Spectrobz 140 — 8y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago
IncreaseStats = coroutine.create(function(player)
    while true do
        wait(10)
        player.leaderstats.Credits.Value = player.leaderstats.Credits.Value + 5
    end
end)

function CreateStats(NewPlayer)
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Value = 0
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Value = 50
    Credits.Parent = leaderstats
    leaderstats.Parent = NewPlayer
    coroutine.resume(IncreaseStats [NewPlayer])
    return leaderstats
end
game.Players.PlayerAdded:connect(CreateStats)

The LocalPlayer variable is a variable that can only be used inside of a LocalScript, which is inside of a Player. I've also changed the Event to .PlayerAdded as it is only being called when a Player was added. And the last, but not least problem is the while true do -loop, which disables the script to do anything else than what's inside this loop. I've put it into a coroutine, which is something like a seperate script, that works paralell to the rest of the script.

Ad

Answer this question