Give leaderstat coins to all players on function

  1. What do you want to achieve? Keep it simple and clear!
    give leaderstat coins to everyone

  2. What is the issue? Include screenshots / videos if possible!
    instead of giving the coins out evenly it gives it to only one player (No Errors)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Devfourms, and youtube videos (even the shorts :sob:)

Code:

--Important VARs
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

--DataStore VARs
local FloorDataStore = DataStoreService:GetDataStore("PlayerFloors")

--Floor VARs
local FloorFolder = ServerStorage.Floors

--Elevator VARs
local Elevator = workspace.Elevator

local ElevatorSpawnPart = Elevator:FindFirstChild("SpawnPart")
local ElevatorDoorA = Elevator:FindFirstChild("DoorA")
local ElevatorDoorB = Elevator:FindFirstChild("DoorB")
local ElevatorBarrier = Elevator:FindFirstChild("Barrier")

local ElevatorPanel = Elevator:FindFirstChild("Panel")
local PanelTimer = ElevatorPanel.Screen.TimerGUI.TextLabel

--Sound VARs
local ElevatorMusic = ElevatorPanel.Screen.Music
local PlaySongEvent = ReplicatedStorage.Remotes.PlaySong
local MusicName = ReplicatedStorage.MusicName
local PanelDing = ElevatorPanel.Screen.Ding


local MusicPlaylist = script.ElevatorMusic:GetChildren()

local PlayerDataFolder = ReplicatedStorage.PlayerData

--Gameloop VARs
local IntermissionTime = 18
local DoorOpenTime = 3

local FloorLoop = math.random(1, #FloorFolder:GetChildren())
local SongLoop = math.random(1, #MusicPlaylist)

local player = nil

game:GetService("Players").PlayerAdded:Connect(function(Player)
	player = Player
end)

local function OpenDoors()
	ElevatorBarrier.SFX:Play()

	while ElevatorDoorA.Position.Z <= 6 do
		ElevatorDoorA.Position += Vector3.new(0, 0, 0.1)
		ElevatorDoorB.Position -= Vector3.new(0, 0, 0.1)
		wait()
	end
end

local function CloseDoors()
	ElevatorBarrier.SFX:Play()
	ElevatorBarrier.CanCollide = true

	while ElevatorDoorA.Position.Z >= 2 do
		ElevatorDoorA.Position -= Vector3.new(0, 0, 0.1)
		ElevatorDoorB.Position += Vector3.new(0, 0, 0.1)
		wait()
	end
end

--Gameloop functions
local function ElevatorTimer(tickValue)
	for i=0, tickValue do
		PanelTimer.Text = tickValue - i
		wait(1)
	end
end

local function Intermission()
	--Play some tunes
	if SongLoop == #MusicPlaylist then
		SongLoop = 1
	else
		SongLoop +=1
	end
	if ElevatorMusic.TimeLength ~= 0 then
		--warn("Musics TimeLength = 0. " ..MusicName.Value)
		ElevatorMusic.SoundId = MusicPlaylist[SongLoop].SoundId
		ElevatorMusic.Volume = MusicPlaylist[SongLoop].Volume
		ElevatorMusic:Play()	

		MusicName.Value = MusicPlaylist[SongLoop].Name
		print("Now Playing: " .. MusicPlaylist[SongLoop].Name)
	else
		warn("Musics TimeLength = 0")
	end
	

	--Wait until timer is up
	ElevatorTimer(IntermissionTime)

	MusicName.Value = "Nothing"
	ElevatorMusic:Stop()
	PanelDing:Play()
end

local BackupSkyboxDebounce = false

function BackupSkybox()
	if BackupSkyboxDebounce == false then
		BackupSkyboxDebounce = true
		warn("SkyBox Not Found. Creating Backup...")
		local backupSkybox = script.SkyboxBackup:FindFirstChild("Skybox"):Clone()
		backupSkybox.Parent = Lighting
		task.wait(5)
		BackupSkyboxDebounce = false
	end
end

local function ClearFloor()
	for i, child in ipairs(Lighting:GetChildren()) do
		if child.ClassName == "Sky" then
			if script:FindFirstChild("Skybox") then
				script:FindFirstChild("Skybox").Parent = Lighting
			else
				BackupSkybox()
			end
		end

		child:Destroy()
	end

if workspace:FindFirstChild("CurrentFloor") then
		workspace:FindFirstChild("CurrentFloor"):Destroy()
else
	warn("Could Not Destroy CurrentFloor")
end
	for i, child in ipairs(PlayerDataFolder:GetChildren()) do
		if child.InElevator.Value == true then
			PlaySongEvent:FireClient(Players:FindFirstChild(child.Name), 0)
			if player ~= nil then
				--print("passing coins")
				player.leaderstats.Coins.Value += 6
				
				local newSound = script.Pickup:Clone()
				newSound.Parent = player.Character:FindFirstChild("HumanoidRootPart")
				newSound:Play()
				newSound.Ended:Wait()
				newSound:Destroy()
				--print("passed giving coins")
			end
		end
	end
	print("Giving Back NPCS")
	if ReplicatedStorage.StoredNPCS:FindFirstChild("NPCS") then
		ReplicatedStorage.StoredNPCS:FindFirstChild("NPCS").Parent = workspace
	end
	
	-- Give Coints
	
end

local function NewFloor(floorId)
	local floor = floorId:Clone()
	floor.Name = "CurrentFloor"
	
	if floor:FindFirstChild("Dangerous") then
		local floorDangerous = floor:FindFirstChild("Dangerous")

		if floorDangerous.Value == true then
			print("Dangerous Floor")
			task.wait(1)
			workspace.NPCS.Parent = ReplicatedStorage.StoredNPCS
		else
			print("Non Dangerous Floor")
		end
	end

-- for all children in lighting folder
	for i, child in ipairs(floor.LightingEffects:GetChildren()) do
		
		if child.ClassName == "Sky" then -- if class is a sky then
			if Lighting:FindFirstChild("Skybox") then -- if we find a name with "skybox"
				Lighting:FindFirstChild("Skybox").Parent = script
			else
				BackupSkybox()
			end
	
		child.Parent = Lighting
		end
	end

	floor.Parent = workspace

	if floor.OpenBarrier.Value then
		ElevatorBarrier.CanCollide = false
	end

	--Play Floor music, if any
	if floor:FindFirstChild("Music") then
		for i, child in ipairs(PlayerDataFolder:GetChildren()) do
			if child.InElevator.Value then
				PlaySongEvent:FireClient(Players:FindFirstChild(child.Name), floor.Music.Value, floor.Music:GetAttribute("Looped"), floor.Music:GetAttribute("StartPos"), floor.Music:GetAttribute("Volume"))
			end
		end

		MusicName.Value = floor.Music:GetAttribute("SongName")
	end

	--Add Floor for Players
	for i, child in ipairs(PlayerDataFolder:GetChildren()) do
		if child.InElevator.Value then
			child.TotalFloors.Value += 1
		end
	end
end

local function CollectPlayers()
	for i, child in ipairs(Players:GetChildren()) do
		if child.Character:FindFirstChild("HumanoidRootPart") and PlayerDataFolder:FindFirstChild(child.Name).InElevator.Value then
			child.Character:FindFirstChild("HumanoidRootPart").CFrame = ElevatorSpawnPart.CFrame
		end
	end
end

--Other stuff
Players.PlayerAdded:Connect(function(player)
	--Create new Player profile
	local newProfile = script.ProfileTemplate:Clone()
	newProfile.Name = player.Name
	newProfile.Value = player.UserId
	newProfile.Parent = PlayerDataFolder
end)

Players.PlayerRemoving:Connect(function(player)
	local playerProfile = PlayerDataFolder:FindFirstChild(player.Name)

	--Save Player data
	if playerProfile then
		--Save Player data
		FloorDataStore:SetAsync(player.UserId, playerProfile.TotalFloors.Value)

		--Remove Player profile
		playerProfile:Destroy()
	end
end)

while true do
	--Elevator loop
	local ElevatorTeam = game:GetService("Teams").Elevator
	
	repeat
		wait()
		if #ElevatorTeam:GetPlayers() ~= 0 then
			-- yes players in elevator
			Intermission()
		else
			-- no players in elevator
			--print("no players on team")
		end
	until #ElevatorTeam:GetPlayers() ~= 0
	

	if FloorLoop == #FloorFolder:GetChildren() then
		FloorLoop = 1
	else
		FloorLoop +=1
	end
	NewFloor(FloorFolder:GetChildren()[FloorLoop])
	--NewFloor(FloorFolder.Mansion)
	if workspace.CurrentFloor.OpenDoor.Value then OpenDoors() end

	ElevatorTimer(workspace:FindFirstChild("CurrentFloor").FloorTime.Value)

	if workspace.CurrentFloor.OpenBarrier.Value == true then
		CollectPlayers()
	end

	if workspace.CurrentFloor.OpenDoor.Value == true then CloseDoors() end
	ClearFloor()
end

Main Lines:
game:GetService(“Players”).PlayerAdded:Connect(function(Player)
player = Player
end)

&

for i, child in ipairs(PlayerDataFolder:GetChildren()) do
	if child.InElevator.Value == true then
		PlaySongEvent:FireClient(Players:FindFirstChild(child.Name), 0)
		if player ~= nil then
			--print("passing coins")
			player.leaderstats.Coins.Value += 6

Do you mean like in-game or like everyone who has data within your game? If it’s in-game you can just loop through the players and increment their leaderstats value by X

everyone who is in the elevator (if child.InElevator.Value == true then) should be given leaderstat coins

and how would i do that? (looping through players)

for	Index, Player in game:GetService("Players"):GetPlayers() do
	Player.leaderstats.Coins.Value += 6
end

Edit: Add this in the section where you give the players in the elevator coins

1 Like

Hey there its working when im in solo, and it gives me 6 coins to my leaderstats.

Testing Two Players Now. Thanks Alot btw

Seems like everything is the same. it gave player1 12 coins (both p1 and p2’s coins)

still broken

Edit! they both have 12 instead of 6. Whys that? PS: my gui just wasnt working.

So are you good or not im confused

It gives 12 instead of 6

som e words

Do you have any other code that rewards coins?
Can you screenshot the code section where you placed the loop?

i have a coin part

Code:

local CoinsAdded = 4 -- Change the number to adjust how much you want the player to receive each time
local RespawnTime = 5 -- Change the number to adjust how often you want the coin to respawn
local Debounce = false

local coin = script.Parent

coin.Touched:Connect(function(touched)
	if Debounce then
		return 
	end

	local Player = game.Players:GetPlayerFromCharacter(touched.Parent)
	if Player then
		local Leaderstats = Player:FindFirstChild("leaderstats")
		local Coins = Leaderstats and Leaderstats:FindFirstChild("Coins") -- Change coins to whatever your currency is called which you want the values to add onto

		if Coins then
			Debounce = true 
			Coins.Value = Coins.Value + CoinsAdded
			coin.Parent = game:GetService("ReplicatedStorage").Coins
			script.Parent.Pickup:Play()
			wait(RespawnTime)
			coin.Parent = workspace
			Debounce = false 
		end
	end
end)

I think it may be better to use UserId instead of playerName in your :FindFirstChild(), but either way, if you could find the player for the remote with this method, you can also use this method for your leaderstats. So you don’t repeat code, you can assign it to a variable like this:

for i, child in ipairs(PlayerDataFolder:GetChildren()) do
	if child.InElevator.Value == true then
		local playerInElevator = Players:FindFirstChild(child.Name)
		PlaySongEvent:FireClient(playerInElevator , 0)
		--print("passing coins")
		playerInElevator.leaderstats.Coins.Value += 6

Okay, now the coin-giving part runs twice because there would be two PlayerDataFolders. To fix this we will need some restructuring.

Replace your entire PlayerDataFolder children loop with this:

for Index, Player in game: GetService( "Players"): GetPlayers() do
	local DataFolder = PlayerDataFolder:FindFirstChild(Player.Name) 
	if not DataFolder then continue end
	
	local InElevator = DataFolder:FindFirstChild("InElevator")
	if not InElevator then continue end
	
	if not InElevator.Value then continue end
	
	PlaySongEvent:FireClient(Player, 0)
	Player.leaderstats.Coins.Value += 6
end

Hey, sorry for being late. It gives both player 6 coins to thier total ammount. Thanks alot for the help!

Hey thanks alot for helping. real;ly happy its working now

1 Like

Before I’m done here… task.wait > wait :stuck_out_tongue_closed_eyes:

ah true

words andth aerfretqeq qw

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.