Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Specific player Updation on my leaderboard script help?

Asked by 6 years ago

This is my script

local function Check(hit)
if hit.Name == "Brick" then
wait(0.2)
hit:Destroy("Brick")
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value + 1 -- Change to how you like it
end
end

script.Parent.Touched:connect(Check)
~~~~~~~~~~

I want to make a tycoon and this is the block deleter script but it gives money to all the players in the server. what I want is to make it give money to the nearest player.

Broken script I tried


local function Check(hit) if hit.Name == "Brick" then wait(0.2) hit:Destroy("Brick") game.Players.Parent.NearPlayer.leaderstats.Money.Value = game.Players.Parent.NearPlayer.leaderstats.Money.Value + 1 -- Change to how you like it end end

script.Parent.Touched:connect(Check) ~~~~~~~~~~~~~~~~~

0
ummmm this is broken? SwagnLag -2 — 6y
0
I can't understand, can you edit what you wrote and be more clear and specific? That's pretty confusing. OfcPedroo 396 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

New solution based on your clarified problem:

Sorry for my last answer not directly addressing your problem!

Though what you have there is good in theory, sadly, Roblox has no way of automatically detecting if a block is within range of another block (unless you have something that measures distance, like one of the new Constraints, attached).

Considering this, what I'd recommend doing is as follows:

Step 1. Take a list of all the players in the server. Step 2. Check the distance of their character's base part (HumanoidRootPart) from the brick. Step 3. Add to the closest player's leaderstats.

To follow these steps:

By calling local playerList = game.Players.GetChildren(), you make a table called Playerlist that will have every player currently in game on it.

Next, you would make a "for" loop, counting through the list of players and checking how far away from the brick each one is.

local playerList = game.Players.GetChildren()
local targetDistance = 100 -- set this number to the distance you want to be the maximum distance a player can be and still get points.
local closestPlayer = nil -- this'll change when we find our closest character.

for i = 1, #playerList do -- "as i counts 1 at a time over the number of things on playerList"
    if (playerList[i].Character.HumanoidRootPart.Position - script.Parent.Position).magnitude < targetDistance then -- if the distance between the brick's position and the current character on our list is lower than our current record/maximum...
        targetDistance = (playerList[i].Character.HumanoidRootPart.Position - script.Parent.Position).magnitude
        closestPlayer = playerList[i]
    end
    if closestPlayer ~= nil then
        closestPlayer.leaderstats.Money.Value = leaserstats.Money.Value +1
    end
end

I haven't had a chance to test this script live, so sorry if you run into any minor errors.

This segment replaces your two lines that refer to game.Players.LocalPlayer. Hope this helps!

0
If you could Simplify all the steps... Im a noob ;( SwagnLag -2 — 6y
Ad

Answer this question