Can you tell me if this script works? There are no errors in the output, but this script doesn’t do anything. I am trying to change a value in a script through another script. I will give you the two scripts.
local AutoSave = 60
local StartAmmount = 0
local DataSaveService = game:GetService('DataStoreService'):GetDataStore(''..CurrencyName..'DataStorage')
game.Players.PlayerAdded:Connect(function(Player)
local success, newData = pcall(function()
return DataSaveService:GetAsync(Player.userId) or StartAmmount
end)
local leaderstats = Player:WaitForChild('leaderstats')
local Currency = Instance.new('NumberValue',leaderstats)
Currency.Name = CurrencyName
Currency.Value = newData
while wait(AutoSave) do
print('Saving '..CurrencyName..'')
local success, ValueAmmount = pcall(function()
return DataSaveService:SetAsync(Player.userId, Currency.Value)
end)
end
end)
This is my leaderstats script, and I want to change the Currency.Value through another script. This is that script.
local Points = game.ServerScriptService.DrumPointsLeaderstats.Currency.Value
local Noise = game.SoundService.Snap
function onMouseClick()
Points = Points + 1
Noise:Play()
end
clickdetector.MouseClick:Connect(onMouseClick)
Does this work on anyone else’s end? Is this a bug? how could I fix it?
I recommend saving the players data as a table if you want the players save to contain more than one value.
I also recommend saving the players data when they leave, so you dont risk going into the queue.
The reason why its not working because the Points variable is being set to the Currency value.
Try using this instead:
local Points = game.ServerScriptService.DrumPointsLeaderstats.Currency
local Noise = game.SoundService.Snap
function onMouseClick()
Points.Value = Points.Value + 1
Noise:Play()
end
It’s fine. I actually changed it to a .Touched function. But every time i add in the value to change, the noise doesn’t play and the script does nothing.
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
script.Parent.Snap.Playing=true
game.ServerScriptService.Leaderstats["Drum PointsLeaderstats"].Currency.Value = game.ServerScriptService.Leaderstats["Drum PointsLeaderstats"].Currency.Value + 1
end
end)
And yes,i know that i should define the leaderstats in a local, but i tried that and the same problem came up
Okay it appears what you’ve told me that its an issue with the line if hit.Parent:FindFirstChild('Humanoid') then
Is the character supposed to be hitting the part?
If so, check if the name of the Humanoid is different.
Try using this instead:
local server_script_service = game:GetService('ServerScriptService')
local leaderstats = server_script_service:WaitForChild('Leaderstats')
local drum_pointsleaderstats = leaderstats:WaitForChild('Drum PointsLeaderstats')
local currency = drum_pointsleaderstats:WaitForChild('Currency')
local part = script.Parent
local sound = part:WaitForChild('Snap')
part.Touched:connect(function(hit)
if hit.Parent:FindFirstChildOfClass('Humanoid') then
sound:Play()
currency.Value = currency.Value + 1
end
end)