Need assistance with a script

Yo, I’m a beginner scripter and I just wanted to ask y’all on here how I could change this script to where it displays a person’s time on the leaderboard even if they’ve died. Right now, it only makes the time show on the leaderboard only if they didn’t die from stage 1-100! Also the game isn’t round based its just my obby with a timer so players can compete! Again, the script only uploads the person’s time on the leaderboard if they don’t die, and I want to change that! I want to make it so that no matter if they die, their time shows up! Let me know if you need any more information like the other scripts I use, the parts’ names, etc!

-- [ SERVICES ] --

local DataStoreService = game:GetService("DataStoreService")

-- [ LOCALS ] --

local DataVer = "DataTimer" -- Data Store Name
local Store = DataStoreService:GetOrderedDataStore("LeaderBoardData2")

-- [ FUNCTIONS ] --

_G.ForceSet = function(plrName, newTime)
	local num = tonumber(newTime)

	num = num * 1000
	Store:SetAsync(plrName, num)
end

game.ReplicatedStorage.ApplyTime.OnServerEvent:Connect(function(plr, newTime) -- On Event
	
	local num = tonumber(newTime)
	local oldData = Store:GetAsync(plr.Name)
	
	if oldData then
		oldData = oldData / 1000
	else
		oldData = 1000
	end
	
	---- ANTI CHEAT (needs fixing)
	if num < oldData then
		if num <= 900.30 then
			plr:Kick("No cheating.")
		else
			num = num * 1000
			Store:SetAsync(plr.Name, num)
		end
	end
	
end)
5 Likes

If it doesn’t work when a player dies, then it’s probably because this whole script runs once? I’m not sure what’s going on here.

1 Like

Hey diamond how’re you? Also I really don’t understand whats going on either, in the script the youtuber I got the time trial model from wrote “Only saves time when player fully completes course without dying.” Since I’m a beginner dev I don’t know how to fix this so is there a way that I can tweak the script into doing the complete opposite which is letting them get a place on the leaderboard even if they die?

It should run more than once because it runs off a OnServerEvent, so the code will run every time the event is ran.
You might just be running the event once still?

1 Like

Waddup Pup, do you know how I can fix that? I have 2 remoteevents called “ApplyTime” and “ResetCheckpoint” Do y’all need me to show more scripts dealing with the timer and the leaderboard?

It should run whenever the remote event is ran. When do you use the remote event?

1 Like

I’m a beginner dev so I’m lost, and I hope I don’t sound rude and/or clueless when I say this. But, what do you mean “when do you use the remote event?”

you used getAsync with player.Name (try using player.UserId)

1 Like

Oh so getAsync(player.UserId) and SetAsync(player.UserId)? Or do I only change the getAsync ones?

Need something like this but for time in your case … (both sever scripts)

--last part of datastore set up
game.Players.PlayerRemoving:Connect(function(player)
	local success, _ = pcall(function()
		DataStore_Coins:SetAsync(player.UserId, player.leaderstats.Coins.Value)
	end)
end)

--other script
local DataStoreService = game:GetService("DataStoreService")
local DataStore_Coins = DataStoreService:GetDataStore("DataStore_Coins")
game:BindToClose(function()
	if not game:GetService("RunService"):IsStudio() then
		local players = game:GetService("Players"):GetPlayers()
		for _,v in pairs(players) do
			local userId = v.UserId
			local success, result = pcall(function()
				DataStore_Coins:SetAsync(userId, v.leaderboard.Coins.Value)
			end)
			if not success then
				warn(result)
			end
		end
	end
	print("EndOfLine")
end)
1 Like

Preciate you for the example mane :sunglasses: :100:

I changed it to this right here, is this cool?

-- [ SERVICES ] --

local DataStoreService = game:GetService("DataStoreService")

-- [ LOCALS ] --

local DataVer = "[DataTimer]" -- Data Store Name
local Store = DataStoreService:GetOrderedDataStore("LeaderBoardData2")

-- [ FUNCTIONS ] --

_G.ForceSet = function(plrName, newTime)
	local num = tonumber(newTime)

	num = num * 1000
	Store:SetAsync(plrName, num)
end

game.ReplicatedStorage.ApplyTime.OnServerEvent:Connect(function(plr, newTime) -- On Event
	
	local num = tonumber(newTime)
	local oldData = Store:GetAsync(plr.UserId)
	
	if oldData then
		oldData = oldData / 1000
	else
		oldData = 1000
	end
	
	---- ANTI CHEAT (needs fixing)
	if num < oldData then
		if num <= 900.00 then
			plr:Kick("No cheating bucko!")
		else
			num = num * 1000
			Store:SetAsync(plr.UserId, num)
		end
	end
	
end)

You really only need to use your datastore twice per player. Once when they load in and once when they log out. It’s nice to also have a catch-all with game:BindToClose(function() for anything that may go wrong. Other than that, you can do all your math/updates with variables … not constantly read/writing to the datastore. If you are updating a leaderboard set that to happen on a timer. Like every 3-5 min you update the leaderboard and datastore if there are any changes. Always looking to minimize your contact with the datastore.

2 Likes

How do I add in a bindtoclose function to the script? Im a beginner so I really don’t know how to make scripts and even when I go to the game “Lua learn” and look for stuff I still can’t find what I’m looking for. Preciate the help though man I’ll see what I can do tomorrow :sunglasses:

That is the whole script … Just make a new server script. It will handle just that.
this looks junky btw, change this to …

local rs = game:GetService("RunService")

if not rs:IsStudio() then

This part is set up so it don’t save the datastore on close in the studio … or don’t use this part of it at all. I quit saving my datastores in studio as it kills them sometimes. Just make sure it’s working right first.

In my case this would be the minimalistic script. This is the crash fail safe.

local DataStoreService = game:GetService("DataStoreService")
local DataStore_Coins = DataStoreService:GetDataStore("DataStore_Coins")
game:BindToClose(function()
	local players = game:GetService("Players"):GetPlayers()
	for _,v in pairs(players) do
		local userId = v.UserId
		local success, result = pcall(function()
			DataStore_Coins:SetAsync(userId, v.leaderboard.Coins.Value)
		end)
	end
end)

Here is an example of a leaderboard datastore doing all I was talking about …
Global-Resetting-Leaderboard This one also resets everything on a set time.
This one is set to every hour.

1 Like

Oh God that hurts my brain. Preciate it and I’ll see what I can do in the morning! :sunglasses: :100:

Also if you want to try out the game its right here! If you do decide to play it, lemme know if your time popped up on the leaderboard even if you died during the obby! :sunglasses: :+1:t4:

If you’re using scripts from other YouTubers or sources, then make sure they actually know what they are doing, even if they seem legitimate at a first glance.

Also it looks like that this script is doing some kind of time-giving? I really have no idea what you’re up to with this script.

Also, hi to you too.

1 Like

Yeah its the main time trial script connected to my leaderboard updater and the actual timer gui. I can send the other 2 if you feel like lookin at em! And what I’m trying to do make the script put a person’s time on the leaderboard EVEN IF they died in the obby. Because right now the unedited script I put at the start of the thread only saved a player’s time if they didn’t die from stage 1-100. I don’t know if the edited one works now because I haven’t tested it but I’ll see! :sunglasses: :100:

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