hello,
I’m trying to change a couple of integer variables in a script to be set to a value which is stored in the server,
Specifically, I am trying to set two walkspeed values for sprinting and walking that scale off of the player’s speed stat, but in order to not make it exploitable, I am storing their values for speed in the server, but I am unsure of how to define them in a local script since they cant access them. Could anyone help?
Thanks
1 Like
You can implement this step by step here
-
Define Speed Values on the Server:
- Assume you have a data storage that keeps track of the player’s speed stat.
- Create a
RemoteFunction for getting the player’s speed values.
-
Retrieve Speed Values in Local Script:
- Call the
RemoteFunction from the client to get the speed values when needed.
Server Script
First, let’s define the server script to handle the speed values:
local Players = game:GetService("Players")
-- Assuming you store speed stats in a player attribute 'Speed'
local function getSpeedValues(player)
local speedStat = player:GetAttribute("Speed") or 0
local walkSpeed = speedStat * 1 -- Replace with your formula for walk speed
local sprintSpeed = speedStat * 2 -- Replace with your formula for sprint speed
return walkSpeed, sprintSpeed
end
local speedFunction = Instance.new("RemoteFunction")
speedFunction.Name = "GetSpeedValues"
speedFunction.Parent = game.ReplicatedStorage
speedFunction.OnServerInvoke = function(player)
return getSpeedValues(player)
end
Players.PlayerAdded:Connect(function(player)
-- Set default speed stat attribute if it doesn't already exist.
if not player:GetAttribute("Speed") then
player:SetAttribute("Speed", 10) -- Example default speed stat
end
end)
Local Script
Next, we create the local script to retrieve and use the speed values:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage")
local function getSpeedValues()
local speedFunction = replicatedStorage:WaitForChild("GetSpeedValues")
return speedFunction:InvokeServer()
end
local walkSpeed, sprintSpeed = getSpeedValues()
-- Assuming you have a function to set the walkspeed of the player
-- Replace 'SetWalkSpeed' and 'SetSprintSpeed' with the actual implementation
local function updatePlayerSpeed(newWalkSpeed, newSprintSpeed)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Function to update walk speed
humanoid.WalkSpeed = newWalkSpeed
-- Here you could add logic for sprinting
-- For example, listening to a key press to switch between walking and sprinting
end
updatePlayerSpeed(walkSpeed, sprintSpeed)
-- Example code to demonstrate toggling between walk and sprint
local UserInputService = game:GetService("UserInputService")
local isSprinting = false
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift then
isSprinting = true
updatePlayerSpeed(sprintSpeed)
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift then
isSprinting = false
updatePlayerSpeed(walkSpeed)
end
end)
1 Like
You can’t hide these numbers from the client while also telling them to the client. Define your walkspeed values in the local script.
Please don’t just copy paste code from ChatGPT. It is neither helpful or appreciated.
I’m not sure what you mean by ‘a value stored on the server’.
Do you mean your value is stored on a Script, and you’re trying to define it from a LocalScript?