Hello, i am unable to add points using remote events.
(Server) Script for the event :
local ReplicatedStorage = game:GetService("ReplicatedStorage") local givepcvalue = ReplicatedStorage:WaitForChild("GivePC") local function givepc(player) local stat = player.leaderstats.PCs.Value print(player.Name .. " fired the remote event") stat = stat + 1 end -- Call "onCreatePart()" when the client fires the remote event givepcvalue.OnServerEvent:Connect(givepc)
(Server) Script that fires the event :
script.Parent.Triggered:Connect(function(plr) game.ReplicatedStorage.GivePC:FireClient(plr) script.Parent.Parent.Parent.Parent:Destroy() end)
(it detects if the proximity prompt has been triggered)
There is no errors in the output and the part correctly gets deleted.
(Server) Script for the leaderstats (if needed) :
game.Players.PlayerAdded:connect(function(p) local stats = Instance.new("IntValue") stats.Name = "leaderstats" stats.Parent = p local money = Instance.new("IntValue") money.Name = "Coins" money.Value = 0 money.Parent = stats local money = Instance.new("IntValue") money.Name = "Gems" money.Value = 0 money.Parent = stats local money = Instance.new("IntValue") money.Name = "PCs" money.Value = 0 money.Parent = stats end)
You cannot plant remote events that send data from server to server, remote events only fire from server to client(s) or client to server. You're triggering the remote event to fire on the Server. Instead make all of the script in ServerScriptService, and a combination of your scripts that will work.
Code Example, Server Sided Proximity prompt.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local ProximityPrompt = game.Workspace.Part.ProximityPrompt -- change this reference local givepcvalue = ReplicatedStorage:WaitForChild("GivePC") ProximityPrompt.Triggered:Connect(function(plr) local stat = plr.leaderstats.PCs.Value -- get PCs Value print(plr.Name) -- Outputs Player Name stat += 1 -- adds 1 to player stat end) givepcvalue.OnServerEvent:Connect(givepc)
I hope this helps!
When you write local stat = plr.leaderstats.PCs.Value
you are actually creating a new variable and not editing the already existing one. That's just how LUA works.
Your code should look like this
plr.leaderstats.PCs.Value = plr.leaderstats.PCs.Value + 1
(In other languages you could do variable++
or variable+=1
, but that's not the case with LUA)
EDIT: Looking at your script, you are calling the function FireClient
. You would do that if you wanted to send data from server to client, and you would listen to it with RemoteEvent.OnClientEvent
. You don't need remote events if you're doing everything on the server, however, the ProximityPrompt.Triggered
event is fired for clients only. Your second script should definitely be a LocalScript, and you should use FireServer
instead of FireClient