A script that will only run when a player joins the game - lua

game.Players.PlayerAdded:Connect(function(player)
game.StarterPlayer.CameraMaxZoomDistance = 0
end)
I want the player to be in first perspective only when they first join the game.
After they die they should be in third person. How do I go about this?

game.Players.PlayerAdded:Connect(function(player)
game.StarterPlayer.CameraMaxZoomDistance = 0
end)
after line 1 and before game.StarterPlayer add a characteraddedfunction like so
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
game.StarterPlayer.CameraMaxZoomDistance = (number you want for default)
end)
end)
this waits for character to respawn and gets that character and puts it in the character variable, then the game waits for the Humanoid object to appear inside the character, then it waits for the Died event to take place which is the event that watches for a respawn or "death" then it sets the cameradistance back to normal based on the number you put.

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))

Keycard door not working when i put the keycard at replicates storage

I have a key card door but it don't work when I put it in replicatesStorage (it is a gamepass key) can somebody help me it only work when put in starterPack it is currently being given with a localScript at StartGui ** here is code:
script.Parent.Touched:Connect(function(hit)
if hit.Parent.Name == "Clearance1" then
script.Parent.CanCollide = false
script.Parent.Transparency = 0.5
wait(0.5)
script.Parent.CanCollide = true
script.Parent.Transparency = 0
end
end)
StarterPack is already replicated, in a way.
If you use ReplicatedStorage, you'll have to add it to PlayerInstance.Backpack.
Also, script.Parent.Touched is server-side only.
If you wanna access the player's backpack, you can use game.Players.PLAYERNAME.Backpack
The way you're doing this is actually not a very good idea. (no offense)
I would recommend leaving the item inside the StarterPack. If you really don't want to, you can programmatically do it by putting it in their Backpack. Like this:
-- Server script
game.Players.PlayerAdded:Connect(function(player) -- Runs when a player joins the game
player.CharacterAdded:Connect(function() -- Runs when that player respawns or their character loads
local itemCopy = game.ReplicatedStorage.Clearance1:Clone() -- Creates a copy of the item
itemCopy.Parent = player.Backpack -- Puts the item in the player's backpack (inventory)
end)
end)
What that code does is: Every time the player spawns, it clones the item and puts it in their inventory.
Now to check if the user has the item when they touch the door, you can do something like this:
-- Server script
script.Parent.Touched:Connect(function(part) -- Activates when a part touches the doorf
local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Gets a player from the part that touched
if player and player.Backpack:FindFirstChild("Clearance1") then -- Makes sure there is a player, and the player has the keycard in their inventory
script.Parent.CanCollide = false -- Makes the part uncollidable
script.Parent.Transparency = 0.5 -- Sets the part to have transparency
wait(0.5) -- Waits half a second
script.Parent.CanCollide = true -- Makes the part collidable
script.Parent.Transparency = 0 -- Makes the part not transparent
end
end)
Every time the part is touched, it checks if it's a player. If it is, it checks if the player has the item. If so, it runs the code.

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.

Roblox Studio Lua: Cash For Kill Script Issue

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.

Resources