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

How do I access player values in runservice.Heartbeat:connect(function()?

Asked by 2 years ago
Edited 2 years ago

I'm trying to make a sprint system that adjusts the max stamina based on a player value, game.workspace.Players.LocalPlayer.Data.MaxStamina.Value, but it says, "attempt to index number with "Data" which from my understanding (not much) means it's trying to access "Data" yet can't, but data is a part of the LocalPlayer, and the exact same line was used earlier and worked. How can I access the value of the MaxStamina?

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runservice = game:GetService("RunService")
local player = game.Players.LocalPlayer
local staminaRegen = 2



local sprintModifier = 3 -- how much the players walk speed is amplified by sprinting
local sprintStaminaCost=1 --how much stamina player uses per frame of sprinting

local sprintingPlayers = {}

players.PlayerAdded:Connect(function(player)
    wait(.01)
    local maxStamina= player.Data.MaxStamina.Value--this works, 
    print(maxStamina)
    local stamina = Instance.new("IntValue",player.Data)
    stamina.Name = "Stamina"
    stamina.Value = maxStamina
    stamina.Changed:Connect(function(property)
        replicatedStorage.Remote.StaminaUpdate:FireClient(player,stamina.Value,maxStamina)
    end)
end)

--sprint key is pressed/released

replicatedStorage.Remote.Sprint.OnServerEvent:Connect(function(player, state)
    local humanoid = player.Character.Humanoid

    if state == "Began" then--and humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude >0  then, this part was part of a guide i followed, but prevented from working, and when removed didnt notice any error related to current issue
        sprintingPlayers[player.Name] = humanoid.WalkSpeed  
        humanoid.WalkSpeed = humanoid.WalkSpeed * sprintModifier

    elseif state== "Ended"and sprintingPlayers[player.Name] then
        humanoid.WalkSpeed = sprintingPlayers[player.Name]
        sprintingPlayers[player.Name]= nil


    end
end)

--adjust stamina for each player every frame
runservice.Heartbeat:Connect(function()--do I have to pass in player, maxStamina or someother value as a param?
    wait(0.1)
    local maxStamina = game.Players.LocalPlayer.Data.MaxStamina.Value--this doesn't
    print(maxStamina,"\n",maxStamina.Value)
    for index, player in pairs(players:GetChildren()) do
        local stamina= player.Stamina.Value
        local name = player.Name
        local maxStamina = player.MaxStamina.Value
        if not sprintingPlayers[name] then
            if stamina.Value > maxStamina then
                stamina.Value = maxStamina
            elseif stamina.Value < maxStamina then
                stamina.Value = stamina.Value + staminaRegen
            end
        else
            if stamina.Value >= sprintStaminaCost then
                stamina.Value= stamina.Value-sprintStaminaCost
            else
                player.Character.Humanoid.WalkSpeed = sprintingPlayers[name]
                sprintingPlayers[name]=nil
            end
        end
    end
end)

here is a screenshot of the location of "Data" https://imgur.com/a/O1I1qRr and the code above is located in ServerScriptService

I've tried to fix this with my very limited knowledge for a couple of hours, so please someone explain how.

0
NeverMind, I figured it out. The lines i was saying dont work were not nessessary as they are accessed in the for loop I guess Awesomeboy991019 0 — 2y

Answer this question