Removing a player's leaderstats if they click a gui on roblox - lua

Some simple code
script.Parent.MouseButton1Up:connect(function()
????.leaderstats.lvl.Value = 0
????.leaderstats.xp.Value = 0
????.leaderstats.gold.Value = 0
It's not even working. So the player clicks the gui, but how can it reset the players leaderstats, specifically lvl, xp, and gold, I run a fairly popular roblox rpg game with about 400 people right now and this would be an immense help.

You could put the following code in a LocalScript inside your button.
Player = game.Players.LocalPlayer --Only works inside a localscript, replace with the line below it if you need it as a Script.
-- Player=script.Parent while (not Player:IsA("Player")) do Player = Player.Parent end
script.Parent.MouseButton1Up:connect(function()
local index, stat
for index, stat in pairs(Player.leaderstats:GetChildren()) do
stat.Value = 0
end
end)

It should be in a LocalScript, so you can just use the LocalPlayer variable to get the leaderstats and set them.

Related

Cloning Locally and not locally [Lua]

I've been trying to make a sword fight game on Roblox studio. I've made a shop Gui so you can click the text button to buy the sword. It works well, You click it checks your kills and if you have enough you get the weapon. In this case, it's 0 kills. But when you take out the sword you cant use it. I've done my research and I believe that's because it's been cloned locally and not globally. Is this the case and if so how do I fix it?
The script in the Text Button:
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Kills.Value >= 0 then
local clonar = game.ServerStorage.ClassicSword:Clone()
clonar.Parent = player.Backpack
end
end)
Thanks in advance!
When work needs to be performed on the server (as opposed to locally on the client), you can use RemoteEvents to communicate across the client-server boundary.
So first, you'll need to create a RemoteEvent in a shared location, like ReplicatedStorage.
Next, update your client-side LocalScript to fire the RemoteEvent :
local player = game.Players.LocalPlayer
local buyEvent = game.ReplicatedStorage.RemoteEvent
script.Parent.MouseButton1Click:Connect( function()
buyEvent:FireServer()
end)
Finally, you need to create a Script in the Workspace or ServerScriptService that listens for that RemoteEvent and performs the work of giving the player the item :
local buyEvent = game.ReplicatedStorage.RemoteEvent
buyEvent.OnServerEvent:Connect( function(player)
if player.leaderstats.Kills.Value >= 0 then
local clonar = game.ServerStorage.ClassicSword:Clone()
clonar.Parent = player.Backpack
end
end)

How do i make 2x cash gamepass script Roblox

So I was making a script that gives you 5 cash every minute, I also made a game pass for the script if someone owns the game pass they get double money as the Non-game pass holders. Here is my script
I Haven't any scripts to give cash but the problem is in the 2nd script block, the console print an error :
  09:10:57.466 ServerScriptService.CashGiver:6: attempt to index nil with 'UserId' - Server - CashGiver:6
local Give5Cash = game.ReplicatedStorage:WaitForChild("Give5Cash")
local Give10Cash = game.ReplicatedStorage:WaitForChild("Give10Cash")
Give5Cash.OnServerEvent:Connect(function()
print("Player Will Be Given 5 Cash")
end)
Give10Cash.OnServerEvent:Connect(function()
print("Player Will Be Given 10 Cash")
end)
while wait() do
local MPS = game:GetService("MarketplaceService")
local id = 16031172
local player = game.Players.LocalPlayer
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
game.ReplicatedStorage:WaitForChild("Give10Cash"):FireServer()
print("Player Owns 2x Cash")
else
print("Players Doesnt Owns 2x Cash")
game.ReplicatedStorage:WaitForChild("Give5Cash"):FireServer()
end
wait(5)
end
local player = Players.LocalPlayer
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
...
Here you assign a nil value to player which you may not index but do.
From the Roblox manual:
Players.LocalPlayer
NotReplicated
This item is not replicated across Roblox’s server/client boundary.
LocalPlayer is a read-only property which refers to the Player whose
client is running the game.
This property is only defined for LocalScripts (and ModuleScripts
required by them), as they run on the client. For the server (on which
Script objects run their code), this property is nil.
So I figured out a way to solve this issue and I have got the answer for my question.
Here is how I did it I made 3 scripts in ServerScriptService with the name of CashGiver5, CashGiver10, and CashGiverHandler
here are the scripts I added to each script.
CashGiver5:
while wait(1) do
print("Giving Player 5 Cash ")
for i, player in pairs(game.Players:GetPlayers()) do
player:WaitForChild("leaderstats").Cash.Value += 5
end
end
CashGiver10:
while wait(1) do
print("Giving Player 10 Cash ")
for i, player in pairs(game.Players:GetPlayers()) do
player:WaitForChild("leaderstats").Cash.Value += 10
end
end
CashGiverHandler:
local MarketPlace = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
local g = 16031172 -- DOUBLE CASH ID
local Give5Script = game.ServerScriptService.CashGiver5
local Give10Script = game.ServerScriptService.CashGiver10
if MarketPlace:UserOwnsGamePassAsync(player.UserId, g) then
Give5Script:Destroy()
else
Give10Script:Destroy()
end
end)
WHAT THE SCRIPT DOES?
So basically the CashGiver scripts are basic scripts giving player Cash every second.
so the Handler script Destroys one of the scripts (s) when a player is added to the game.

attempt to index nil with 'leaderstats' in roblox studio

local garbage = game.Teams["Glizzy Garbage"]
local player = game.Players.LocalPlayer
if player.leaderstats.Pounds.Value <= 1000 then --this is the line that the output is detecting the error
player.Team = garbage
end
I am trying to make it where when the player reaches a certain amount of 'pounds' then they will automatically receive a roll. I've searched through many youtube videos and haven't found a fix or an alternative way to do this, and I'm not sure why this isn't working. This script is located in the workspace. All help is appreciated.
The LocalPlayer object is only exposed in LocalScripts. Because you're using a Script in the Workspace, you'll have to access the player object another way. It's also a good idea to handle this kind of logic inside a function that is fired any time the Pounds value changes.
Try using the game.Players.PlayerAdded signal :
local garbage = game.Teams["Glizzy Garbage"]
game.Players.PlayerAdded:Connect(function(player)
local Pounds = player.leaderstats.Pounds
Pounds.Changed:Connect(function(value)
if value <= 1000 then
player.Team = garbage
end
end)
end)
Mine is just
local plrStage = plr.leaderstats.Stage.Value
try instead of putting team name put team colour
also use
player.Team.Service = garbage
end)

How to fix an end expected error near eof

I made a cash for kill script in Roblox, but wanted to go further than that and implement a script where when a player has a gamepass, then that player would recieve more cash than another normal player would.
This is for a battle royale styled game in Roblox, and when I playtested it, there were no errors, but the script didn't work.
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
local increment = 50
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,7382818)then
increment = increment + 50
end
player:WaitForChild("Humanoid").Died:connect(function()
local tag = player.Humanoid:FindFirstChild("creator")
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + increment
end
end
end
end)
I expected a VIP player, who has the gamepass, to receive more cash, but when I tested it, no player received any cash for killing another player at all.
How to fix an end expected error near eof
If the Lua interpreter complains about a missing end you`re missing it somewhere.
Read through your code and make sure everything that is supposed to be closed with an end has one. Read through the Lua Reference Manual to find out which keywords need an end.
In your code it's if statements and function definitions. Checking each pair from inside out you'll end up one end) short to close this game.Players.PlayerAdded:connect(function(player) as mentioned in the comments.

Roblox: Make an NPC immovable?

I'm currently working on a game on ROBLOX that contains a TON of NPC's. I need a way to make the player not be able to move them around in any way. I've tried anchoring the HumanoidRootPart, and that worked, but it made the NPC unable to move.
Can anyone help?
You could weld the NPC to the ground, if possible.
This could work:
local weld = Instance.new("WeldConstraint")
weld.Part0 = part
weld.Part1 = otherPart
weld.Parent = part
There is more info here on welding.
If you don't need players to clip through said NPCs, you could "unclip" the NPC, allowing players to move through it but not move it.
local function setDescendantCollisionGroup(Descendant)
if Descendant:IsA("BasePart") and Descendant.CanCollide then
-- set collision group
end
end
model.DescendantAdded:Connect(setDescendantCollisionGroup)
for _, descendant in pairs(model:GetDescendants()) do
setDescendantCollisionGroup(descendant)
end
This should be able to be done using a property . Create a startercharacter so players wear this character,and then modify it's customphysicalproperties "friction" , "density" to a very low number.
You should also be able to put such in a script such as when a player join,the children with class "Part" have their density and friction low.
Try something like this:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 69420
It should make the NPC alot heavier!
And when you want it to move:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 10
That will make the NPC lighter!
So this is the final script:
local NPC = workspace.NPC --The NPC variable
local humanoid = NPC:WaitForChild('Humanoid') --NPC's humanoid
local hrp = NPC.HumanoidRootPart --NPC's HumanoidRootPart
local mass1 = 69420
local mass2 = 10
humanoid.Running:Connect(function(speed)
if speed > 0.001 then
hrp.Mass = mass2
else
hrp.Mass = mass1
end
end)
Make sure to write that code in a ServerScript!
And put the Script inside the NPC if you want.

Resources