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

[REUPLOADED] how do i increase my leaderstats?

Asked by 4 years ago

Hi! I got a Touched event which I want a part called "DirtyRock" to be hit/h (the parameter of touched event) and when that happens, I want the DirtyRock to be destroyed and add a more value to the leaderstats.Money.Value. Here is my script: (it doesn't work)

local RemovePortal = script.Parent
local player  = game.Players.LocalPlayer
RemovePortal.Touched:Connect(function(hit)
    local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part
    if hit == DirtyRock then
        DirtyRock:Destroy()
        player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
    end
end)

RemovePortal is the script that will have the event in it meaning its RemovePortal.Touched (keep in mind the script is in RemovePortal)

3 answers

Log in to vote
1
Answered by 4 years ago

Players.LocalPlayer only works in a LocalScript. If you call It in a regular Script, It will return nil.

I suppose you're trying to make a script where each player gets money when the "DirtyRock" gets touched by "RemovePortal". In that case, you should try this:

local RemovePortal = script.Parent
RemovePortal.Touched:Connect(function(hit)
    local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part
    if hit == DirtyRock then
        DirtyRock:Destroy()
    for _,player in pairs (game.Players:GetPlayers()) do
            player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
    end
    end
end)
0
But that would increase the amount for every player, not just the player which touched the part. ForgotenR4 68 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

It appears you are trying to use game.Players.LocalPlayer in a server script try this script:

local RemovePortal = script.Parent
RemovePortal.Touched:Connect(function(hit)
    local player  = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part
    if hit == DirtyRock then
        DirtyRock:Destroy()
        player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
    end
end)
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

create a script inside the ServerScriptService and add this;

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bin = Instance.new("RemoteFunction", ReplicatedStorage)
Bin.Name = "Add"

local function add(player)
    player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
end
Add.OnServerInvoke = add

inside a LocalScript in the UI

local Add = game:GetService("ReplicatedStorage"):WaitForChild("Add")
local RemovePortal = script.Parent
local player  = game.Players.LocalPlayer
RemovePortal.Touched:Connect(function(hit)
    local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part
    if hit == DirtyRock then
        DirtyRock:Destroy()
        Add:InvokeServer()
    end
end)

Answer this question