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

How can I compare a player's score to another player to see which one dies ?

Hello! Im trying to compare a players score to another player but im not sure how because I dont know how to get their scores in the same script
I have this script in ServerScriptService to make a leaderboard:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Example of a IntValue
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
 
end
game.Players.PlayerAdded:Connect(onPlayerJoin)

I add in a player's score using a part with this script inside whenever they touch the part:

local part = script.Parent
local Players = game:GetService("Players")
curplayers = game.Players:GetChildren()

local function givePoints(player)
    local playerStats = player:WaitForChild("leaderstats")
	local playerPoints= playerStats:WaitForChild("Points")
	playerPoints.Value = playerPoints.Value + 10
	part:Destroy()
end

local function partTouched(otherPart)	-- Gets the player if one touched the part
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		givePoints(player)
	end
end

part.Touched:Connect(partTouched)

I have a block welded on the players that whenever that player comes in contact with another player, (i need help with this=>)a script inside the block needs to check which one of those 2 players have the lowest score and kills him. .

This is the script in ServerScriptService that welds the block , which block will have the script inside that checks which player has the most points and kills the one with the lowest:

local charAdded = function(char)
	local a = script:WaitForChild('hitbox'):Clone() -- Hitbox should be changed to whatever the part name is
	local w = Instance.new('Motor6D')
	w.Part0 = char:WaitForChild('UpperTorso') -- Change this to whatever part of the character you wish to weld it to
	w.Part1 = a
	w.Parent = char['UpperTorso'] -- This should be the same part as above
	a.Parent = char
end

game:GetService('Players').PlayerAdded:Connect(function(plr)
    if plr.Character then
        charAdded(plr.Character)
    end

    plr.CharacterAdded:Connect(charAdded)
end)

So do you guys have any ideas how i could compare the score of the 2 players?

Thank you SO much for your time!!!!!

1 answer

✓ Accepted answer

I see what your trying to make.

You can do this in the script in the hitbox.

local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") and touch.Parent.Name ~= script.Parent.Parent.Name then -- Check to see if the touched part is a child of a player and is not this hitbox's player
		local plr = Players:GetPlayerFromCharacter(script.Parent.Parent) -- Get the player of this hitbox
		local otherPlr = Players:GetPlayerFromCharacter(touch.Parent)
		if otherPlr.leaderstats.Points < plr.leaderstats.Points then
			touch.Parent.Humanoid.Health = 0
		else
			script.Parent.Parent.Humanoid.Health = 0
		end
end)

5 comments

0

Thanks for helping! But im getting an error : Workspace.Player1.hitbox.THIS IS THE SCRIPT I CANT MAKE:6: attempt to index nil with 'leaderstats' Could you help me fix it?

0

Fixed!

0

wow you re quick! It worked! Thanks so much for being so helpful have a great day!

0

You too! Enjoy the rest of your morning / evening.

0

Thank you so much!

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