Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Unable to add points using remote events. How to fix?

Asked by
Komas19 34
2 years ago

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)

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Hello,

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!

0
Attempt to connect failed: Passed value is not a function (line 11) Komas19 34 — 2y
Ad
Log in to vote
0
Answered by
Miniller 562 Moderation Voter
2 years ago
Edited 2 years ago

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

Answer this question