LocalScript
local UIS = game:GetService("UserInputService") local NPC = workspace:WaitForChild("NPC") local event = game.ReplicatedStorage.Substract local CurrentNPC = game.ReplicatedStorage.CurrentNPC local player = game:GetService("Players").LocalPlayer local char = player.Character or player.CharacterAdded:wait() local maxDistance = 10 UIS.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.E then if UIS:GetFocusedTextBox() == nil then for i, v in pairs(NPC:GetChildren()) do local mag = (v.HumanoidRootPart.Position - char.HumanoidRootPart.Position).magnitude if mag <= 10 then CurrentNPC.Value = v.Name event:FireServer() end end end end end end)
Server Script
local event = game.ReplicatedStorage.Substract local NPC = workspace.NPC local currentNPC = game.ReplicatedStorage.CurrentNPC event.OnServerEvent:connect(function(player) local stats = player:WaitForChild("leaderstats") local present = stats:WaitForChild("Presents") if present ~= nil then if present.Value >= 1 then present.Value = present.Value - 1 print(currentNPC.Value) end end end)
When I print(currentNPC.Value) which is in the ServerScript, it just prints blank. But when I checked the value for the CurrentNPC in the ReplicatedStorage it is the name of the NPC for example "Test". When I print the currentNPC in the LocalScript it does show the name of the NPC.
When a server Script
modifies an object in ReplicatedStorage
, that change replicates to all clients. But when a client LocalScript
modifies an object in ReplicatedStorage
, that change does not replicate anywhere outside that client (and the change will probably be discarded the next time the server sends an update for it).
So do any important changes in the server Script
, by passing any needed data through your RemoteEvent
:
LocalScript:
... if mag <= 10 then event:FireServer(v.Name) end ...
Script:
... event.OnServerEvent:connect(function(player, npcName) local stats = player:WaitForChild("leaderstats") local present = stats:WaitForChild("Presents") if present ~= nil then if present.Value >= 1 then present.Value = present.Value - 1 currentNPC.Value = npcName print(currentNPC.Value) end end end)