generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

local subject = "A very confusing block of code"

Previous Thread :: Next Thread 
minerioProgramer is not online. minerioProgramer
Joined: 21 Dec 2012
Total Posts: 432
10 May 2016 09:34 PM
--Here is the problem:

--_______________________________________________________________________
script.Parent.Touched:connect(function()
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Hits.Value = Player.leaderstats.Hits.Value + Player.leaderstats.Level.Value
game.Workspace.Serverhits.Value = game.Workspace.Serverhits.Value + Player.leaderstats.Level.Value
end
end
end)
--________________________________________________________________________
--This script is wonderful when there is only one player on the server.
--But slow to realize, someone joined my game.
--I hit the ball.
--We both got a point.

print "BUT THATS NOT ALL"

--For some straaanngggeee reason, he got one point, and I got 6 points.
--I was leaderstats.Level.Value = 6
--That's normal, right?
--But why didn't the other guy get 6 points, if hitting the ball gives us both points?

--BTW the script is in the ball part in the workspace in the game.
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
10 May 2016 09:39 PM
because you made a loop to give everyone in the server a point when it's touched

script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Hits.Value = Player.leaderstats.Hits.Value + Player.leaderstats.Level.Value
game.Workspace.Serverhits.Value = game.Workspace.Serverhits.Value + Player.leaderstats.Level.Value
end
end
end)

Sorry for the lack of indentation
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
10 May 2016 09:40 PM
d=false
script.Parent.Touched:connect(function(hit)
if d==false then d=true
if game.Players:GetPlayerFromCharacter(hit.Parent) then
plr=game.Players:GetPlayerFromCharacter(hit.Parent)
plr.leaderstats.Hits.Value=plr.leaderstats.Hits.Value+1
wait(1)
d=false
end
end
end)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
10 May 2016 09:42 PM
This is because you loop through all players in the game, and you add their Level to their Hits stat.

His level was 1, so he got 1 point. Your level was 6, so you got 6 points.

Let's look here:



for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Hits.Value = Player.leaderstats.Hits.Value + Player.leaderstats.Level.Value
game.Workspace.Serverhits.Value = game.Workspace.Serverhits.Value + Player.leaderstats.Level.Value
end
end



Assume 2 players in the game:

minerioProgramer
AgentFirefox



This is the process of the loop:


for each player ...
Player: minerioProgramer
if minerioProgramer has leaderstats, then
add minerioProgramer's level to his Hits stat
end if

loop

Player: AgentFirefox
if AgentFirefox has leaderstats, then
add AgentFirefox's level to his Hits stat
end if

loop

! No players left !
end for



So as you can see, your loop adds points to each player based on his own level.
Report Abuse
minerioProgramer is not online. minerioProgramer
Joined: 21 Dec 2012
Total Posts: 432
10 May 2016 09:46 PM
Thank you guys, I didn't see that dumb mistake I made.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image