I have been trying to make this code that checks if a value has changed, and then returns this new value to the server, but I don’t get any output and nothing seem to change.
After this question has been answered, I’ll go further into details
print("-- Player Event Has Started --")
warn("-- Player Event Has Started --")
local Player = script.Parent.Parent
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins") -- the players coins
local EventCoins = game:GetService("DataStoreService"):GetOrderedDataStore(game:GetService("ReplicatedStorage"):WaitForChild("EventDataStoreName")) -- gets data from data store
local success,Data = pcall(function()
return EventCoins:GetAsync(Player.UserId)
end)
local Eventcoins = Data
if EventCoins == nil then
EventCoins = 0
end
---
OldValue = PlayerCoins.Value -- Player's current coins
PlayerCoins.Changed:Connect(function(NewVal)
print("connected to Changed event")
local Difference = NewVal - OldValue
print(Difference)
OldValue = NewVal
end)
To answer your first question, no, server scripts will not run if placed inside the ‘StarterPlayerScripts’ container, a player’s ‘PlayerScripts’ folder is exclusively for local scripts and by extension module scripts required by local scripts. To fetch the ‘local’ player from a server script placed inside the ‘StarterCharacterScripts’ container you should use the following code.
local Game = game
local Script = script
local Players = Game:GetService("Players")
local Character = Script.Parent
local Player = Players:GetPlayerFromCharacter(Character)