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

Roblox server script increment more than expect ?

Asked by 2 years ago

it happend in Roblox Studio Testing when this code run at this point instead of increase Agility.Value by 1 it increase base on player in the server

Ex if there are 3 player the Agility.Value is increase by 3 instead of 1

----------------------------Server Script--------------------------------------

local DataStoreService = game:GetService("DataStoreService") local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

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

local PlayerStat = Instance.new("Folder")
PlayerStat.Name = "PlayerStat"
PlayerStat.Parent = player

local Agility = Instance.new("IntValue")
Agility.Name = "Agility"
Agility.Parent = PlayerStat
Agility.Value =0

local Strength = Instance.new("IntValue")
Strength.Name = "Strength"
Strength.Parent = PlayerStat
Strength.Value =0   

local playerUserId ="Player_"..player.UserId

local data


local success, errormessage = pcall(function()
    data = MyDataStore:GetAsync(playerUserId)
end)    

if success then
    if data ~= nil then
        Strength.Value = data.Strength
        Agility.Value = data.Agility
    else
    Strength.Value = 0
    Agility.Value = 0
    end


    print("Load success")
else 
    print("Saved is F")
    print(warn)
end

game.ReplicatedStorage.Agilityplus.OnServerEvent:Connect(function(Player)
    Player.PlayerStat.Agility.Value += 1
end)
game.ReplicatedStorage.Strengthplus.OnServerEvent:Connect(function(Player)
    Player.PlayerStat.Strength.Value += 1
end)

end)

game.Players.PlayerRemoving:Connect(function(player) local playerUserId ="Player_"..player.UserId print(playerUserId)

local data = {
    Agility = player.PlayerStat.Agility.Value;
    Strength = player.PlayerStat.Strength.Value;
}
print(data)
local success, errormessage = pcall(function()
    MyDataStore:SetAsync(playerUserId,data)
end)
if success then
    print("Saved")
else
    print("data is screw")
    print(warn)
end

end) ----------------------------Client Script-------------------------------------- local player = game.Players.LocalPlayer local Agility = player:WaitForChild("PlayerStat").Agility local TextLable = script.Parent local buttonplus = script.Parent.Parent.Plus

buttonplus.MouseButton1Click:Connect(function() game.ReplicatedStorage.Agilityplus:FireServer(player) end)

TextLable.Text ="<b>AGI: "..Agility.Value.."</b>"

Agility.Changed:Connect(function() TextLable.Text ="<b>AGI: "..Agility.Value.."</b>" end)

0
Move your OnServerEvents outside of your PlayerAdded. 9mze 193 — 2y

Answer this question