Need a little assistance with a script!

Anybody know how to make this script do the complete opposite of the 2nd sentence in the brackets? Basically I want the script to let the player have a time on the leaderboard regardless if they die from start to end! People have said “datastore this, bindtoclose that” and nothing’s worked!:sunglasses::+1:t4:

This is the script in ServerScriptService

--[[
    This script will apply the leaderboard time, and check if a player has finished or completed the obby.
    Only saves time when they fully complete course without dieing.
--]]

-- [ SERVICES ] --

local DataStoreService = game:GetService("DataStoreService")

-- [ LOCALS ] --

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

-- [ 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, please!")
        else
            num = num * 1000
            Store:SetAsync(plr.UserId, num)
        end
    end
    
end)

We would need to know what script is calling the ApplyTime remote event, as the newTime value is what needs to be modified.

1 Like

The script I sent above is the script in serverscriptservice so here are the other 2! You’re welcome! :sunglasses:
This is the script inside of the leaderboard part that updates the leaderboard:

local DataStoreService = game:GetService("DataStoreService")

-- [ LOCALS ] --

local DataVer = "DataTimer" -- Data Store Name

local Store = DataStoreService:GetOrderedDataStore("LeaderBoardData5")

local RationalDigits = 3 -- How many numbers show.

-- [ FUNCTIONS ] --

function GetLBData()

	local success, pages = pcall(function()
		return Store:GetSortedAsync(true, 10)
	end)

	if success and pages then
		local topTimes = pages:GetCurrentPage()
		for rank, data in pairs(topTimes) do
			local val = data.value / 1000

			local decimalOffset = math.pow(10, RationalDigits)
			local RoundedTime = math.round(val*decimalOffset)/decimalOffset
			local splitComplete = tostring(RoundedTime + (1 / (decimalOffset * 10))):split(".")
			local PrettyTime = splitComplete[1] .. "." .. splitComplete[2]:sub(0, 3)

			script.Parent.SurfaceGui["A" .. tostring(rank)].PlrName.Text = data.key .. ": " .. PrettyTime
		end
	end
end

GetLBData()

while wait(60) do
	pcall(function()
		GetLBData()
	end)
end

And this is the local script inside of the timerGUI in the screenGUI thing:

--[[
	This script handles the touch/stop plates for timer leaderboard.
--]]

-- [ SERVICES ] --

local RunService = game:GetService("RunService")

-- [ LOCALS ] --

local par=script.Parent

local timer=par:WaitForChild'Timer'
local timerlabel=timer:WaitForChild'Time'

local SetTime = tick()

local RationalDigits = 3 -- How many numbers is shown.

local timer_active=false
local timer_time=0

local prev

-- [ FUNCTIONS ] --

function connectplrtouch(pt,func)
	pt.Touched:Connect(function(t)
		local c=game.Players.LocalPlayer.Character
		if c and t:IsDescendantOf(c) then
			func()
		end
	end)
end

for _,d in pairs(workspace:GetDescendants()) do
	
	if d.Name=='TimerStart' then
		local name=(d:FindFirstChild'Title' and d.Title:IsA'StringValue' and d.Title.Value) or ''
		connectplrtouch(d,function()
			par.Enabled=true
			if prev~=d then
				SetTime = tick()
				prev=d
			end
			--timerlabel.Visible = true
			--timer.Visible = true
			timerlabel.TextColor3=Color3.new(1, 1, 1)
			SetTime = tick()
			timer_active=true
		end)
	end
	
	if d.Name=='TimerEnd' then
		connectplrtouch(d,function()
			timerlabel.TextColor3=Color3.new(0.172549, 1, 0.027451) -- Finish color
			if timer_active == true then
				game.ReplicatedStorage.ApplyTime:FireServer(timerlabel.Text)
			end
			timer_active=false
		end)
	end
	
end

local Accuracy = 10^RationalDigits

function TimerFunc()
	if timer_active then

		local Div1 = math.abs(tick() - SetTime)
		local CalculatedIncrement = math.round(Div1*Accuracy)/Accuracy
		local Addons={}
		for t=1,RationalDigits do
			local i = t-1
			local integer,predecimal = math.modf(CalculatedIncrement*(10^i))
			local decimal = predecimal/(10^i)
			if decimal == 0 then
				Addons[t] = "0"
			end

		end
		local NewText = tostring(CalculatedIncrement)
		for i,v in pairs(Addons) do
			NewText = NewText..v
		end
		timerlabel.Text=NewText	

	end
end

while true do
	wait(.025)
	TimerFunc()
end

If you look in the properties of the ScreenGui, there will be something about reseting on spawn. If you disable that, does it result in the desired outcome?

1 Like

ResetOnSpawn has been disabled ever since I got the model, but it just doesn’t work either. I know I’ve been annoying people on here, but I’ve tried everything and nothing seems to work

If you want it to save when they die.

You can locate the code section related to saving the time (ApplyTime) remote event, and event which triggers it (touched event) and add in a new event which triggers on death (humanoid.Died event).

1 Like

Oh I’m not sure about what I should do in that portion of script and I can’t find it! I have a feeling you were probably talking about the gui one since I see “textlabel” Like I said I’m a beginner and I’m very careful on where stuff goes! :sunglasses:

I found the section of code you were talking about! Now I just gotta go in studio and hope I don’t mess anything up lol. I don’t know what to do, what do I change, add? Where?

Lol what happened, what’d you say?

Can you give a better explanation? Like did you mean if they got to the end, but died; it would still show the time it took them to get there as the best time or what?

1 Like

Nah the complete opposite! If they died during the obby from start to finish they don’t get a time on the leaderboard. But if they don’t die at all they do! If you wanna join the game and test it yourself you can! I’ll let you fly to the end yourself😎

All good, I think I understand. Doing this has multiple routes, like detecting if the CharacterRemoved (Most Popular), or check if the Humanoid died, etc. On the server or client (however you handle the times, most likely client then sending it to the server, anyways). Detect once the Character is removed then stop the time and set it to 0 or whatever.

1 Like

LOL IT JUST WORKED BUT IT SHOWS THE IDS on THE LEADERBOARD INSTEAD OF THE PLAYER’s username!

1 Like

That’s an error inside the leaderboard retriever, you have it setting the UserIds instead of the names.

1 Like

So do I change store:GetAsync(plr.UserId)
And store:SetAsync(plr.UserId, num) to plr.Name or just the set async?

No, keep it as plr.UserId otherwise if a player changes their name, they lose their datastore.

1 Like

Gotcha! I’ll just keep the leaderboard as playerID then!:sunglasses::+1:t4:

1 Like

Alright I just got 2 people to test and it doesn’t work now. What in God’s name is goin on? I just wish there was a way that I could rework the serverscriptservice script so it would allow the player to have a time even if they die but everything seems so complicated

Where ever you are setting the text for the leaderboard, do this : game.Players:GetNameFromUserIdAsync(data.key)
Instead of data.key

This will allow you to get the player’s name from their userId even if they are not in the current server.

2 Likes

Preciate it bacon mane but I went on and removed the leaderboard from my game for now! I’ll put it back as soon as I get the solution for the leaderboard though