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

Script only works for one player at a time in my game?

So essentially, I have a script ServerScriptService. Inside this script, there's a function for when a player is added, and within that a function for when character is added. In the character added function, another function is called outside of both functions, called RandomFunction, that tracks how many steps the player takes, which works fine. The problem is, when there is more than one player in the game, only one player's steps are being tracked. If that player stops moving, the next player to move will have their steps tracked, and it will continue to track only their steps until they stop moving and someone else starts moving.

The question is, will I have to move the script somewhere? I'm not keeping track of stats locally then firing a remote to update the stats globally because that would cause too many remotes to fire based on how often the stat is updated. What am I to do?

Thank you!

edit (code): (NOTE: This is under ServerScriptService)


local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		
		repeat wait() until player.Character
		 
		player.CharacterAdded:Connect(function(char)  
			randomfunc(char, player) 
			print('Char added function loaded')
		end)
		
		
end)


function randomfunc(charac, player)
		print('Random function for steps added loaded')
		
			local stepStat = player.leaderstats["Steps Taken"]
			charac.Humanoid.Running:Connect(function(speed)
				
			    local newWalking = speed >= 1
			    if newWalking == walking then return end --no change
				
			    walking = newWalking
				while walking do
					wait(.3)
					stepStat.Value = stepStat.Value + 1
				end
				--print(walking)
			   
			end)
end

1 answer

function randomfunc(charac, player)
	spawn(function() -- just in case that was the problem
	local s,e = pcall(function()
        print('Random function for steps added loaded')

            local stepStat = player.leaderstats["Steps Taken"]
            charac.Humanoid.Running:Connect(function(speed)

                local newWalking = speed >= 1
                if newWalking == walking then return end --no change

                walking = newWalking
                while walking do
                    wait(.3)
                    stepStat.Value = stepStat.Value + 1
                end
                --print(walking)

            end)
	 end)
	if s then
	print('successful')
	else
	print('it didnt work, recode it so it doesnt error.')
	end)
end
workspace.ChildAdded:Connect(function(x)
	if x:FindFirstChild("Humanoid") then
		randomfunc(game.Players[x.Name].Character, game.Players[x.Name]) 
	end
end)

I really hate calling functions that are created a few lines ahead of the code so I chose to keep the function at the back, when an object is added to workspace, it determines if it is a character then it calls randomfunc. Also you don't wait for the character to load then tell it if it is added at that point in time because, that will only run once.

2 comments

0

Why workspace.ChildAdded?

0

it detects more parts, it iterates more and unlike character added, the character loads once but parts can detect more..

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