Hi! I am making a GUI where you enter a players user ID and it will give them 10 points.
When I click “Submit” it gives me the output
‘355661302’ Prints the user ID.
I am using a Remote Function to do this.
Server script used to do the remote function stuff:
local Event = game.ReplicatedStorage:WaitForChild("GivePoints")
local val = game:GetService("Lighting").Value.Value
local player = game:GetService("Players"):GetPlayerByUserId(val)
Event.OnServerEvent:Connect(function()
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 10
end)
The local script to fire the remote event:
local Event = game.ReplicatedStorage:WaitForChild("GivePoints")
local player = script.Parent.Parent.TextBox.Text
script.Parent.MouseButton1Click:Connect(function()
Event:FireServer(player)
print(script.Parent.Parent.TextBox.Text)
game:GetService("Lighting").Value.Value = script.Parent.Parent.TextBox.Text
end)
Lighting is used to hold the player’s ID inside of an IntValue, I try and use this intvalue in the server script to give the points, i’m still unsure whether that’ll work or not.
Thanks!
(Ps. I might give late replys as it is 2 am and I’m going to sleep now.)
local player = game:GetService("Players"):GetPlayerByUserId(val) returns a reference to a player object. If the code runs before the player connects to the game, the function will return nil, which it appears it may be doing in your case. If you move the player variable declaration into the OnServerEvent connection, it should be fine.
I did it like this and it didn’t work, I don’t think I did it right?
local Event = game.ReplicatedStorage:WaitForChild("GivePoints")
local val = game:GetService("Lighting").Value.Value
Event.OnServerEvent:Connect(function()
local player = game:GetService("Players"):GetPlayerByUserId(val)
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 10
end)
I decided to take a look at your script again and i saw some errors.
The client script is very wrong, You are trying to get the player from a text.
Copy this and paste it in the Server script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
end)
local Event = game.ReplicatedStorage:WaitForChild("GivePoints")
Event.OnServerEvent:Connect(function(player)
player:WaitForChild("leaderstats").Points.Value = player:WaitForChild("leaderstats").Points.Value + 10
end)
In the client:
local Event = game.ReplicatedStorage:WaitForChild("GivePoints")
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerByUserId(script.Parent.Parent.TextBox.Text)
Event:FireServer(player)
end)
You should always do a bit research before making a topic.
-- In 'ServerScriptService', add this into a script named 'PlayerSetup'
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Display an 'IntValue' on leaderboard
local gold = Instance.new("IntValue")
gold.Name = "Points"
gold.Value = 100
gold.Parent = leaderstats
end
-- Run 'onPlayerJoin()' when the 'PlayerAdded' event fires
game.Players.PlayerAdded:Connect(onPlayerJoin)
Hi, I’ve been testing it and when I type numbers in the text box (player ID) it gives me (Local Player) the points. I have tested this with my friend, and when I typed his ID in the text box, it gave me the points. But when I type letters for example, it doesn’t give points to anyone.
So I can’t give points to the players. Any idea why it does this? By the way, it sends no output.
(PS. I am using this in an admin panel, maybe thats why? I doubt it is the reason why.) (The crossed out stuff is irrelevant, they’re just for tweening the GUIs ect.)
This is because the first parameter of the .OnServerEvent is the player who fired the event. To fix this just add another parameter to the function. Also, you can just do += 10 instead of typing the path again.