I want to make a command that would kill a player you specify..
Let's say I type: kill/Paul. Now I want to kill the player with the name Paul.
This is my command Script:
local player = ...
game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
player.Chatted:connect(function(message) --this function executes when the player type into chat
--commands are here
if player.Name == "TominoCZ" or player.Name == "nathancain" or player.Name == "block100000" then
if message == "kill/me" then
player.Character.Head:remove()
end
if message == "ff/me" then
if player.Character:findFirstChild("ForceField") then
player.Character.ForceField:Destroy()
end
Instance.new("ForceField").Parent = player.Character
end
if message == "unff/me" then
if player.Character:findFirstChild("ForceField") then
player.Character.ForceField:Destroy()
end
end
end
end)
end)
Now you can see that I already have a command that will kill the play that executed it.
But how can I kill a different player by specifying the player's name after the "kill/"?
This command script might look too long or not too pro, but atleast I know and understand what it does.
So any ideas?
You can do something like this:
local player = ...
game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
player.Chatted:connect(function(message) --this function executes when the player type into chat
--commands are here
if string.sub(message,1,string.len("kill/"))=="kill/" then --check the message if it starts with command "kill/"
if string.sub(message,string.len("kill/"))==player.Name then --kill the player with the name
player.Character.Head:remove()
end
end)
end)
I don't know Lua so there might be syntax errors, but the overall idea is that you use string.sub method to divide your message into 2 parts: the command part and the info part. If the command part equals to "kill/", then find the player with the name specified in the info part, and kill him! (Or behead him... I don't play ROBLOX :D)
Related
This script is designed so that when you say "!kick [user]" it kicks that user from the game. But, when I test it out, nothing happens with no errors in the output box. Whats going on?
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(player,message)
if player.Name == "playername" then
local words = string.split(message," ")
if string.lower(words[1]) == "!kick" then
words[2]:Kick("You have been kicked.")
end
end
end)
end)
Thanks
The reason nothing is happening right now is because your very first check is failing. You have the parameters for the Player.Chatted connection backwards. The message comes first and the recipient comes second. So what you've named player is actually the message and if player.Name == "playername" then is likely failing because strings don't have a Name property and player.Name is nil.
After that, the next issue you will run into is that after you've split the command into two parts, the second half of the command is still a string, and not a Player. So you need to find a Player with a matching name as the input string.
Try something like this:
local Players = game.Players
local function findPlayerByName(name)
-- escape if nothing is provided
if name == nil then
return nil
end
-- check if any players match the name provided
local allPlayers = Players:GetPlayers()
for _, player in ipairs(allPlayers) do
if player.Name == name or string.lower(player.Name) == name then
return player
end
end
-- couldn't find any players that matched
return nil
end
-- define all the players that can use commands
local admins = {
["playername"] = true,
}
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
if admins[player.Name] then
local words = string.split(message, " ")
local cmd = string.lower(words[1])
if cmd == "!kick" then
local playerName = words[2]
local targetPlayer = findPlayerByName(playerName)
if targetPlayer then
targetPlayer:Kick("You have been kicked.")
end
end
end
end)
end)
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.
I'm making a gui that shows up when a player interacts with a proximity prompt, but, i want the script to check if the player has the tool in his inventory. If it has the tool then do nothing (don'
t show the gui), if it doesn't have the tool then fire an event. I tried making it but this error keeps showing up Workspace.Part.Script:6: attempt to index nil with 'Backpack'
Here's the script:
debounce = true
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
if debounce then
debounce = false
local noob = game.Players:GetPlayerFromCharacter(player.Parent)
local Tool = noob.Backpack:FindFirstChild("Gunball")
if Tool == nil then
game.ReplicatedStorage.RemoteEvent:FireClient(player)
debounce = true
end
end
end)
Here's the gui script (local), even if i don't really think that is usefull..:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
script.Parent.Visible = true
end)
Workspace.Part.Script:6: attempt to index nil with 'Backpack'
local Tool = noob.Backpack:FindFirstChild("Gunball")
Here noob is nil.
So in local noob = game.Players:GetPlayerFromCharacter(player.Parent) game.Players:GetPlayerFromCharacter(player.Parent) returns nil.
According to the Roblox documentation
This function returns the Player associated with the given
Player.Character, or nil if one cannot be found. It is equivalent to
the following function:
local function getPlayerFromCharacter(character)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character == character then
return player
end
end
end
So it seems that the Parent of player, player.Parent is not a Character associated with any player.
Why should a player property like Character be a parent of a player? I'm no Roblox expert but that doesn't seem to make any sense to me.
If you want to check wether the player who triggered the ProximitPrompt has some item, why not work with player? I mean that's the player who triggered it. So check its backpack, not some parent character stuff.
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.
i'm making a script that detects a person using their name however I can't seem to get it working. Please also point out any mistakes I have done in my script it would help a lot.
game.Workspace:WaitForChild("Console")
print("Waited")
game.Players.PlayerAdded:Connect(function(plr)
print("Connected")
if game.Workspace.Console and plr.Name == "wojciechpa2007" then
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.Startergui
print("Cloned")
elseif
not game.Workspace.Console and plr.Name == "wojciechpa2007" then
plr.Startergui.Console:Destroy()
print("Destroyed")
end
end)
Heyo,
This script has a race-condition in it. Your first line game.Workspace:WaitForChild("Console") will block execution of the rest of your script until the object is loaded, or a timeout is reached.
This means that it is possible that a player could join the game before the script can listen for the game.Players.PlayerAdded signal.
Also StarterGui does not exist on specific player. It exists at the game level and is a bucket that dumps its stuff into a Player's PlayerGui when that player's character loads into the game.
So to fix your script, you could try something like this :
-- right away connect to the PlayerAdded signal
game.Players.PlayerAdded:Connect(function(plr)
print("Player Joined!", plr.Name, plr.UserId)
-- do something special if wojciechpa2007 joins
if plr.Name == "wojciechpa2007" then
print("wojciechpa2007 joined! Adding console!")
-- add the special console into the player's PlayerGui for when they load
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.PlayerGui
end
end)
Some recommendations and things to be careful about here :
It's safer to check a player's UserId than it is to check their name. Roblox lets you change your name, but you UserId is always the same.
Putting something into your StarterGui will make it show up in your PlayerGui the next time your character loads. But if your character is already loaded, you won't see it until the next time you respawn.
If your Console object is some kind of GUI element, make sure that it is parented to a ScreenGui object before you insert it into the Player. Otherwise it just won't show up.
Hope this helps!