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

How DO I Make A Tool Add Leaderstats?

Asked by 3 years ago

so i have been scripting for 2 weeks and my tool isnt working in game but it is in studio also can i get help adding animation?

LeaderStats Code Its Supposed To Save Data This is normal script

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Muscle = Instance.new("IntValue")
    Muscle.Name = "Muscle"
    Muscle.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        myDataStore:GetAsyn(player.UserId.. "-Muscle")
    end)

    if success then
        Muscle.Value = data
    else
        print("There Was A Error Saving Your Data")
        warn(errormessage)  
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()  
        myDataStore:SetAsync(player.UserId.."-Muscle",player.leaderstats.Muscle.Value)
    end)    

    if success then
        print("Player Data Is Saved :D")
    else
        print("There Was An Error Saving Data")
        warn(errormessage)
    end

end)

and here is my tools code this is local script

script.Parent.Activated:Connect(function()
    local plr = game.Players:FindFirstChild(script.Parent.Parent.Name)
    if plr then
        local stats = plr:WaitForChild("leaderstats")
        local Muscle = stats:WaitForChild("Muscle")
        Muscle.Value = Muscle.Value + 1
    end
end)

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You would have to use remote events to pass from the client to the server, a server script cannot detect changes made by a local script

edit: Also you spelled Async wrong in the "GetAsync" function

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The issue is due to the fact that you're changing the values on the client. Because of filtering enabled, it prevents changes on the client to replicate to the server.

In this case, you can change the script to a server script without having to change any of the code and it will still work. If there are other things depending on the local player in your script where changing it to a server script will break it, then you can also use a Remote Event as an alternative solution.

0
what do u mean? spideyweartideys 7 — 3y
0
Changes from the client cant be detected on a server script, theyre saying to either disable Filtering Enabled (a property of workspace) or use Remote events Omq_ItzJasmin 666 — 3y
0
You can't disable filtering enabled as it's forced upon all games, disabling it wouldn't do anything. xInfinityBear 1777 — 3y
Log in to vote
0
Answered by 3 years ago

The problem is exactly because you're doing it in a local script. Local scripts only change the information for that person in particular, in order for it to be changed to everyone you'll need to do it in a server script.

To fix this particular case you'll need to add a "RemoteEvent" into the replicated storage. After that you'll (inside of the local script) do RemoteEvent:FireServer(1) and now on a server script you'll add the event to listen to this request and add the muscle points there.

I highly recommend you to read this in order to understand on how it works: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

0
so could explain it easyier for me to know and maybe write the scripts 4 me spideyweartideys 7 — 3y

Answer this question