I want to change leaderstats values using a Gui and remote events.
The script I made is not working and not showing any errors in the output.
I don’t know what to do and have tried searching for similar scripts that do this.
local rep = game:GetService("ReplicatedStorage")
local remote = rep:WaitForChild("ChangeLevel")
local function getPlayerFromName(name)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Name:lower() == name:lower() then
return player
end
end
end
remote.OnServerEvent:connect(function(Player)
local AdminNameValue = Player.PlayerGui.CE.BG.PlayerName.Text
local AdminLevelValue = Player.PlayerGui.CE.BG.Level.Text
local name = getPlayerFromName(AdminNameValue)
if name then
name.leaderstats.levels.Value = AdminLevelValue
end
end)
Maybe this script is the problem? No idea.
local RepStorage = game:WaitForChild("ReplicatedStorage")
local Remote = RepStorage:WaitForChild("ChangeLevel")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
script.Parent.MouseButton1Down:connect(function()
if script.Parent.Parent.Level.Text ~= "" and script.Parent.Parent.PlayerName.Text ~= "" then
Remote:FireServer(Mouse.Hit)
end
end)
Can someone explain to me what I’m doing wrong and why it’s not changing the value?
Have you tested to make sure that the remote event is called?
Another tip is to never use name as identification of a player. the player has a PlayerId property that you should use instead since that value is always unique. In your code the names: “brad” and “BRAD” would be treated as the same person which could lead to problems.
An example would be if “andy” was an admin but then a player called “AnDy” joins. The script could return the player of “andy” and then treat “AnDy” as the admin.
Your issue is that you’re getting the text from the server rather than the client to fix it all you have to do is, modify your client script to take the Player.PlayerGui.CE.BG.PlayerName.Text and Player.PlayerGui.CE.BG.Level.Text send send those with the RemoteEvent so on the client it’d be :
local AdminNameValue = LocalPlayer.PlayerGui.CE.BG.PlayerName.Text
local AdminLevelValue = LocalPlayer.PlayerGui.CE.BG.Level.Text
remote:FireServer(AdminNameValue,AdminLevelValue)
and on the server it’d be :
remote.OnServerEvent:Connect(function(Player,AdminNameValue,AdminLevelValue)
local name = getPlayerFromName(AdminNameValue)
if name then
name.leaderstats.levels.Value = AdminLevelValue
end
end)
Though you should consider adding sanity checks because exploiters could easily abuse this.