Hello,
I have a local script and a normal script. The other day, someone gave me a datastoring intvalue script for a gun, and I ran into a problem. They told me to make sure the value saves, change it on the server. So I have it on a server script, but it isn’t changing or printing any errors. Here’s the script:
local tool = script.Parent
local clipsize = tool.ammo.Value
tool.Ammo.OnServerEvent:Connect(function(player)
if tool.maxammo.Value - (clipsize - tool.ammo.Value) >= 0 then
tool.maxammo.Value = tool.maxammo.Value - (clipsize - tool.ammo.Value)
end
end)
Could you debug your code further to see what the problem is? Run some print statements to see what evaluations are being made or what parts of code are and aren’t running. A lack of errors shouldn’t be the end of your debugging. Additionally since there’s not much context it’s difficult to give you any meaningful help – this code already looks correct, so not much to comment on it.
Weird, all print statements go through, but the value just…isn’t updating. I don’t know why this doesn’t work, as it makes no sense why changing values just doesn’t work on server scripts?
I read over your code again just to check if I myself skipped over anything as well as whipped up a quick repro using your exact code and I noticed that your source of error seems to come from your calculations. Look at this:
tool.maxammo.Value - (clipsize - tool.ammo.Value)
The equation in the brackets is evaluating to 0. clipsize and ammo.Value are the same, so they cancel out (1 - 1 = 0) and 0 is being subtracted from maxammo. So your values aren’t changing because they have no reason to change; it’s subtracting 0.
Which of these values is supposed to change, exactly? The variable clipsize is a constant (unchanged value) of whatever ammo.Value is at the time of declaration (so if it’s 999, in this script it’ll always be 999 unless you explicitly change or reassign clipsize).
The value that’s supposed to change is tool.maxammo.Value. What is weird is that these exact lines work in a local script perfectly, but don’t in a normal server script.
Is the LocalScript changing any of the values like ammo.Value? The server won’t see that change so it’d explain why a client can work the same lines but the server can’t. Subtract ammo.Value from the server instead of the client so that it can properly evaluate clipsize - tool.ammo.Value to a non-zero.
local players = game:GetService("Players")
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local toolName = "Tool" --Change to name of tool.
local valueName = "Value" --Change to name of value.
local valueName2 = "Value2" --Change to name of value.
local playerToolValues = {} --Cache.
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local backpack = player:WaitForChild("Backpack")
local tool = backpack:WaitForChild(toolName)
local value = tool:WaitForChild(valueName)
local value2 = tool:WaitForChild(valueName2)
playerToolValues[player] = value
end
player.CharacterAdded:Connect(onCharacterAdded)
local character = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")
local tool = backpack:WaitForChild(toolName)
local value = tool:WaitForChild(valueName)
local value2 = tool:WaitForChild(valueName2)
playerToolValues[player] = {value, value2}
local success, result = pcall(function()
return datastore:GetAsync("Tool_"..player.UserId)
end)
if success then
if result then
value.Value = result[1]
value2.Value = result[2]
end
else
warn(result)
end
end
local function onPlayerRemoving(player)
local values = playerToolValues[player]
playerToolValues[player] = nil
local success, result = pcall(function()
return datastore:SetAsync("Tool_"..player.UserId, {values[1].Value, values[2].Value})
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
I changed the original script to include a second value.