Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come this script only works for one person and not the other players?

local time = 1

while true do
	wait(1)
	time = time - 1
	if (time == 0) then
		time = 10
		players = game.Players:getChildren()
		for i=1, #players do
			if (players[i]:findFirstChild("leaderstats")) then
				if (players[i]:findFirstChild("leaderstats")) ~= nil then
				if (players[i]:IsInGroup(2534776)==true) then
					players[i].leaderstats.Ducat.Value = players[i].leaderstats.Ducat.Value + 25
					players[i].leaderstats2.Ducat2.Value = players[i].leaderstats2.Ducat2.Value + 25
				else
					players[i].leaderstats.Ducat.Value = players[i].leaderstats.Ducat.Value + 20
					players[i].leaderstats2.Ducat2.Value = players[i].leaderstats2.Ducat2.Value + 20
				end		
			end
		end
	end
	end

Hello, thank you for reading. In this script, what I'm trying to do is a timed script where after every 10 seconds, you get 25 Ducats added into your leaderstats. When I go play in the server, it only gives money to the very first player who enters the server and not the rest who come later. If someone can help me, I would be so grateful! Thank you.

1 answer

✓ Accepted answer

Hello there! Well to begin try simplifying the script to a much easier use than the generic for loop, refer to this link for some help with loops. The one I will be referring to is the second example in the for loop section. This is a way to go through an array/table without the more complicated for i = 1, number do loop (First Example).

To start lets start by simplifying the while loop.

local PointTime = 10 -- This is the loop time, better than subtracting numbers!

while wait(PointTime) do
end

So, to explain what I just did, is instead of using a bit more complicated generic* while loop, I replaced the true statement with a wait. This runs the loop everytime the wait reaches the time we set :)

local PointTime = 10 -- This is the loop time, better than subtracting numbers!

while wait(PointTime) do
	for num, obj in pairs(game.Players:GetChildren()) do
		if (obj:IsA("Player")) then
			-- Add stuff Soon
		end
	end
end

You may be wondering," What Just Happened!" Let's take this slow and I'll explain this. So what we added is a for loop, which cycles or goes through the entire Players part of the game. The num refers to the current index, the number of the obj based off the array/table. The obj is the Object currently being scanned*. So the next thing is dealing with the obj:IsA("Player") bit. This returns true if the object is in the class "Player". Now lets check if the player has the correct leaderstats.

local PointTime = 10 -- This is the loop time, better than subtracting numbers!
local PointsGiven = 20 -- The amount of points to give!

while wait(PointTime) do
	for num, obj in pairs(game.Players:GetChildren()) do
		if (obj:IsA("Player")) then
			if (obj:IsInGroup(2534776)) then
				PointsGiven = PointsGiven + 5
			end
			if (obj:findFirstChild("leaderstats") and obj.leaderstats:findFirstChild("Ducat")) then
				obj.leaderstats.Ducat.Value = obj.leaderstats.Ducat.Value + PointsGiven
			end
		end
	end
end

To explain what we just added, simply is making sure the player has the correct things and see if the player is in the specific group. The checking of the group won't be explained, as it seems you understand it, but go here, for a refresher. The if statement is used to make sure both the leaderstats and Ducat are in the there and aren't nil.

If you still need the second leaderstats, I'll leave that to you so you get some hands on work. If this doesn't work, please tell me what happened :)

4 comments

0

For Line number 6 it says "Workspace.Script:6: 'in' expected near '='." Is there something wrong?

0

Hmm let me test this further and I'll report back asap

0

Okay, and thank you so much for the explanation. I'm sure I'll put your help to good use

0

Ah, I've found the problem. The post has been updated and should work with no errors now :)

This discussion is preserved as it was. New answers, comments, and votes are closed.