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

Minigame awarding points problem?

Asked by 9 years ago

I used this script for my minigames

minigames = {}

for i,v in pairs (game.Lighting:GetChildren()) do
   if v:IsA("Model") then
      table.insert(minigames,v)
   end
end

function playerAdded(player)
    local Id =  Instance.new("IntValue")
    Id.Name = "leaderstats"
    local points =  Instance.new("IntValue",Id)
    points.Name = "Money"
    Id.Parent = player
end

function playerWon(player,points)
local m = Instance.new("Message", workspace)
    m.Text = player.Name.." has won the minigame!"
    local money = player.leaderstats.Money
    money.Value = money.Value + points
    wait(3)
    m:Destroy()
end

function chooseMinigame()
    return minigames[math.random(1,#minigames)]
end

function getParticipants()
   return game.Players:GetPlayers()
end

function teleportPlayers(players,position)
   for _,v in pairs(players) do
      if v.Character then
         v.Character:MoveTo(position)
      end
   end
end

function loadMinigame(mg)
   local m = mg:clone()
   m.Parent = workspace
   return m
end

function runMinigame(m)
   if m:FindFirstChild("pos") then
      teleportPlayers(getParticipants(),m.pos.Position)
   end

   while m.time.Value > 0 do
      --TODO: check to see if game is running
      if m.winner.Value then
         playerWon(m.winner.Value,m.points.Value)
         break
      end
      wait()
   end
end

function endMinigame(m)
   m:Destroy()
end

Game:GetService("Players").PlayerAdded:connect(playerAdded)


while wait() do
   wait(10)
   local m = chooseMinigame()
   local loaded = loadMinigame(m)
   runMinigame(loaded)
   endMinigame(loaded)
   print(3)
end

The problem is, when you touch a brick, it doesn't award points like it should. I do get something in output.

Workspace.main:21: attempt to perform arithmetic on global 'points' (a nil value) 16:26:23.738 - Stack Begin 16:26:23.739 - Script 'Workspace.main', Line 21 - global playerWon 16:26:23.739 - Script 'Workspace.main', Line 56 - global runMinigame 16:26:23.740 - Script 'Workspace.main', Line 74 16:26:23.740 - Stack End

And whenever I touched the brick, the whole script breaks. Someone please help.

1 answer

Log in to vote
0
Answered by
samfun123 235 Moderation Voter
9 years ago

Your problem is that your not defining how many points the play should get.

To fix this problem I recommend adding another variable called points to the "playerWon" function.

Fixed function :

function playerWon(player,points)
local m = Instance.new("Message", workspace)
    m.Text = player.Name.." has won the minigame!"
    local money = player.leaderstats.Money
    money.Value = money.Value + points
    wait(3)
    m:Destroy()
end

If you want to award points to a player you would call the function like this :

playerWon(game:GetService("Players").FamousDoge,100)

That would give "FamousDoge" 100 points.

If you have any questions, concerns or just need some help with this PM me on ROBLOX!

Ad

Answer this question