Roblox Studio Lua: Cash For Kill Script Issue - lua

So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. I've already got a script, see below. I tried everything on the Internet, but NOTHING works. Even the ones from the toolbox. But instead of giving the killer the cash, it gives the killed person cash! I don't want it to be a death for kill script! Here is the code I have another question like this, it will not help. It wasn't useful(It has the leaderboard, and the killed person gets +10 cash.):
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the
player died.
end
end
end)
end)
end)
Thanks in advance and I hope you guys can help!

There's no way to determine that player A killed player B without keeping track of that information manually.
For example, if player A shoots a gun, the bullet that it spawns could have an added ObjectValue with a Value of player A's humanoid.
When the bullet collides with player B, you would have an easy reference back to player A and can credit them for the KO.

Related

Roblox: how to place a player in a specific position

I'm a newbie developer of roblox.
I'm trying to place a player in a specific position on first load in this way:
In StarterPlayer > StarterPlayerScripts I added a LocalScript with the following code:
local cf = CFrame.new(500, 5, 50)
local Char = game.Players.LocalPlayer
Char.HumanoidRootPart.CFrame = cf
When I click play, nothing happen onload.
What I'm doing wrong?
#ItsJustFunboy is half correct, you do need to use the Model:MoveTo function, but you might have some other issues as well : timing and LocalScripts.
Timing-wise, you have this code in StarterPlayerScripts. These LocalScripts execute when the Player joins the game, not when their character model appears. So it's possible that their character doesn't even exist at the time you're telling it to move. If you want to guarantee that the character model exists at the time you run this code, it needs to be in StarterCharacterScripts.
As for the problem with LocalScripts, they make changes to the world only on your client. Meaning that if you move your character in a LocalScript, then all the other players will still see your character model where it was, not where you moved it. So unless this is intentional, if you want everyone to see this change, you need to move the character model in a server-side Script.
So in a Script in the Workspace or in ServerScriptService, try this :
-- wait for a player to join the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for their character to load into the game
player.CharacterAdded:Connect(function(character)
-- yield the thread for but a moment so that the character can finish loading
wait()
-- move their character
local targetPosition = Vector3.new(500, 5, 50)
character:MoveTo(targetPosition)
end)
end)
use the MoveTo() function:
local plr = game.Players.LocalPlayer
plr.Character:MoveTo(CFrame.new(500, 5, 50))

Is there someway to respawn an NPC in roblox studio?

So, I'm coding this fighting game in roblox studio. Now I have made an NPC, but I want to script it to be able to respawn a few seconds after dead, but I don't know how to check if an NPC is dead or how to respawn one and make it join its body parts again.
Thank you, and I hope you can help.
You can check the health of the NPC from the Health property of its Humanoid in order to see if it's still alive, or if it's dead.
game.workspace.<NPC>.Humanoid.Health
Now in order to check if it's dead, you can do:
if game.workspace.<NPC>.Humanoid.Health <= 0 then
--NPC is dead; respawn
end
In order to 'respawn' the NPC, duplicate the NPC (in workspace) and place the copy in ReplicatedStorage. Now whenever the NPC died, you can replace it with a new one by cloning the one in ReplicatedStorage to workspace:
if game.workspace.<NPC>.Humanoid.Health <= 0 then
wait(2) -- If you want to wait before the NPC respwns, change '2' for the amount of seconds you want to wait
game.ReplicatedStorage.<NPC>:Clone().Parent = game.workspace
end
Finally, wrap this all in a while loop so it can continuously check the health of the NPCs.
while wait() do
if game.workspace.<NPC>.Humanoid.Health <= 0 then
wait(2) -- If you want to wait before the NPC respwns, change '2' for the amount of seconds you want to wait
game.ReplicatedStorage.<NPC>:Clone().Parent = game.workspace
end
end

In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000 cash

In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000
cash,and tool dispears.I already have made a leaderboard for the cash,and already have my tool in server storoge.
First off, you can start by using a .Touched event which fires every time an object is touched. A .Touched event can carry one parameter; the object that touched it. For example, if a player (R15) stepped on a block on the floor, the object that hit the part might be Left Foot. Here is what the skeleton of a .Touched function looks like:
<Part>.Touched:Connect(function(hit)
print(hit.Name) --As per the example above, this would print "Left Foot"
end)
Since any object can touch the part, but not every one has a Backpack, you'll want to first check if the object that hit your part is from an actual player. You can do this by looking for a Humanoid, which would be a sibling of the part that touched. You can do this by adding:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print(hit.Name) --As per the example above, this would print "Left Foot"
end
end)
Now that we have verified that a player was what touched our part, we can use the function of game.Players called GetPlayerFromCharacter(). A Character is just your player in game.workspace, so by calling this function, it gets your player in game.Players from your character. This is similar to game.Players:FindFirstChild(<Player>.Name). Either one work, but I will be using the first one for this example. Added to your code, here is what it should now look like:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
end
end)
As you may know, Backpack is a direct child of the player in game.Players, so we can use player.Backpack to find our Backpack. Now we can do the same thing that we did to check for a humanoid, to check for our tool:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Backpack = player.Backpack
if Backpack:FindFirstChild("<Tool Name>") then
player.leaderstats.<Money>.Value += 2000 --Gives them 2000 cash
end
end
end)
If you cloned the tool into the player's Backpack previously, you can do Backpack.<Tool Name>:Destroy() to get rid of it/make it dissapear.

Fixing my color touch script for my roblox puzzle game

So I have a roblox puzzle game that I have been working on for a while now on based on the arcade classic Q bert where the goal is to change all the colours of the bricks while avoiding enemies and getting a high score but I will be adding some features of my own so it does not get as repetitive such as additional tasks like collecting keys on platforms to unlock the door to the next level and secrets like a diamond that rarely appears once every 10 rounds and collecting one gives the player an extra dude and 10 million points.
This is how the game looks so far https://streamable.com/na46cu
The issue I am having as you can see is that the colours do in fact change but when I jump on it again it changes back to the first colour it changes to which in this case is green but I want it to stay on the first colour and make it so that it does not change until the player jumps on the brick again and later on in the game I want it to become more complex and puzzle like as the game goes on like in this example [https://www.youtube.com/watch?v=9eXJWiNXpOo][2] .
I have tried a few things like adding timers ,debounces and even separate scripts alltogether none of which have worked out for me so far and I of course went out looking for a question from somebody else that had a similar problem but so far I have been struggling to find anybody else that has the same problem.
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
for _, part,brick in pairs (CollectionService:GetTagged("blocks"))
do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if (hit.Parent:FindFirstChild("Humanoid"))
then
part.BrickColor = BrickColor.new ("Bright green")
wait (2)
part.BrickColor = BrickColor.new ("Eggplant")
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module
I am not the best programmer but I do know the basics of programming and I have tried out various programming languages like Python and c++ all of which are not all that hard to understand once you know the basics of it all but finding out the solution to the problem is the really tricky part and so is bug fixing and troubleshooting.
I do know I could try a simple debounce system but that still does not solve the problem and it only makes it so the code only runs once and slows it down.
I have been asking all over the place for a solution to this problem but I never got an answer to it so I am trying on good old Stackoverflow for once to see if this will be the place where I get the help I need.
this should work, try it
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
local DidParts = {} -- Initializing another table to check if the part is already in it
for _, part,brick in pairs(CollectionService:GetTagged("blocks")) do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if hit.Parent:FindFirstChild("Humanoid") then
if table.find(DidParts,part) then
return -- checking if the part isnt in the table if it is then return
end
part.BrickColor = BrickColor.new("Bright green")
wait(2)
part.BrickColor = BrickColor.new("Eggplant")
table.insert(DidParts,part) -- when all the code has finished insert it in the table
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module

Roblox Studio Lua: Making A Cash For Kill Script

So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. The name on the leaderboard should be Cash. Can someone please make a script for this? I've tried everything on the web, so I hope you guys can help. Please include the leaderboard Cash in the script. Thanks in advance!
Update
I've included the code for the script, but instead of giving me the cash, it gives the killed person cash. How can I fix this?
Here is the code:
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the player died.
end
end
end)
end)
end)
You increase the amount of money in currency1. But currency1 belongs to player, who is the who has .Died!
Assuming creator is an ObjectValue whose Value is the Player instance of the killer, we can get that player's "Cash" to increase:
....
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 + 10
end
end
end
......
This is a bit of a complex task if you have no experience with scripting. This requires your weapon system to keep track of who kills, and feed that into the leaderboard.
If you use default Roblox weapons, this functionality is likely already in your weapons. If you are making custom weapons, you need to implement this yourself.
I suggest you check out the Roblox Wiki for an example of Leaderboards. This is a relevant article about Leaderboards.
There are also going to be plenty of leaderboards in the Toolbox that you can edit to your needs. The default Leaderboard found in the "Roblox Sets" section of the Leaderboard increases a counter named "Kills" upon kills (with default Roblox weapons), and you can edit this to increase Cash instead. However - again - depends on how you keep track of your kills.
Next time please provide code and/or more detailed description of what you have already tried, so it is easier for us to help you understand or give you pointers. Right now it looks like you haven't really searched on your own and just posted your question here right away.

Resources