Lua Roblox respawn character dead - lua

How should I make this code work? The Judoon character must regenerate, (respawn) when I click on the wall but only when he is dead.[![Regenerate][1]][1] When I click on the wall, my Judoon character has to respawn but only when he is dead.
Also how can I introduce Regen Button, the script, be separate from the Judoon model, but it should work. If you put the script Regen into Workspace, separate from the Judoon folder, it will not work.
local box = script.Parent
local debounce = false
-- DO NOT GROUP THIS WITH YOUR MODEL!
local everything = {Judoon}
local names = {Judoon}
local children = game.Workspace:children()
for i=1,#children do
if (children[i].Name == "Judoon") then -- Replace the name with your models's name.
table.insert(everything, children[i]:clone())
table.insert(names, children[i].Name)
end
end
function regen()
for i=1,#everything do
game.Workspace:findFirstChild(names[i]):remove() -- Dont mess with this stuff.
new_thing = everything[i]:clone()
new_thing.Parent = game.Workspace
new_thing:makeJoints()
end
end
function onClicked()
if Judoon:FindFirstChild("Judoon"):GetState() == Enum.HumanoidStateType.Dead then
regen()
end
end
wait(15)-- This is how long it takes untill the regen button will work again.
script.Parent.BrickColor = BrickColor.new(104)
debounce = false
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
--This regen button was made by andymewborn,hope you like(d) it!
[1]: https://i.stack.imgur.com/HhLo3.png

You can do something like this, if the humanoid is dead then regenerate.
function onClicked()
if Judoon:FindFirstChild("Judoon"):GetState() == Enum.HumanoidStateType.Dead then
regen()
end
end
New edit:
--[[
1. Put this script inside the button
2. Rename the modelname
3. The model must be a child of Workspace
--]]
button = script.Parent
modelname = "Pk" -- Model name
model = game.Workspace:FindFirstChild(modelname)
backup = model:Clone()
function Regen()
local Old =game.Workspace:FindFirstChild(modelname)
Old:Destroy()
local New = backup:Clone()
model = New -- new changes made here
New.Parent = workspace
New:MakeJoints()
end
function onClicked()
if button.BrickColor == BrickColor.new("Bright violet") then
if model:FindFirstChild("Humanoid") ~= nil then
if model.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
Regen()
print("removed and added")
end
end
button.Regen:Play()
button.BrickColor = BrickColor.new("Really black")
wait(3)
button.BrickColor = BrickColor.new("Bright violet")
end
end
button.ClickDetector.MouseClick:Connect(onClicked)

Related

Not giving output When running LocalScript inside of StarterGui

So, Im tring to make a Mind Control script inside of Roblox. Im using the local script so i can use the UserInputServer Module. But When I Run It(Its inside of StartGui), I doesn't give any output. Here is the source:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
game.Players.PlayerAdded:Connect(function(plr)
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
print(Enemy)
Enemy.CFrame = Enemy.CFrame + CFrame.new(0,1,0)
wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)
end)
The statements are only printed after game.Players.PlayerAdded:Connect(function(plr) detects a player being added. Due to the code running in a LocalScript, the player will have already been added by the time the script starts waiting for players. If a second player joins the game, "Player Added!" will indeed be printed in the first player's console.
The player can instead be indexed by using local plr = game:GetService("Players").LocalPlayer.
The script still errors when the player's mouse is pointing to nothing and when trying to change the Enemy's position, but these can be easily fixed. The script then lifts objects upwards by hovering over them and pressing E.
Final code:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
local plr = game:GetService("Players").LocalPlayer
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
if Enemy then --Make sure the player's mouse isn't pointing to nothing
print(Enemy)
Enemy.CFrame += Vector3.new(0,1,0)
end
task.wait(0.5) --More accurate than wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)

How can I send arguments through to a client from server using RemoteEvent? ROBLOX

My goal here is to send the player object and the object name through to the client script to display the object name on a text label.
When I run this, it prints nil and nil. I want player.Name and name as you will see in the second code sample. Another error I get is "attempt to concatenate nil with string" from the client script.
Here is my server-side code:
script.onTouch.OnInvoke = function(button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
if isLoadedPart == true then
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
end
if buyObj ~= nil then
buyObj.Parent = builds
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
end
local ui = game.ReplicatedStorage:FindFirstChild('onPartLoad')
if ui then
ui:FireClient(player, 'buyObj.Name')
print('yes')
else
print('no')
end
else
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local money = info.player.Value.leaderstats.Money
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if money.Value >= buyObjValue or money.Value == buyObjValue then
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
money.Value += sellObjValue
end
if buyObj ~= nil then
buyObj.Parent = builds
money.Value -= buyObjValue
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
warn(td.boughtButtons)
end
else
player.PlayerGui.inGame.error.label.invokeScript.errorInvoke:Invoke("Insufficient Funds")
end
end
script.Parent.waveChecker.afterRun:Invoke()
end
And here is my client-side code:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(player, name)
print(player.Name, name)
print(script.Parent.Text)
script.Parent.Text = name .. 'is loaded.'
print(script.Parent.Text)
end)
Here I will tell you a little about this game. It is a tycoon that saves data using a table with all button Ids in it. When it loads, it gets the button associated with the id and fires the server code for every button. If the button is a load button, it fires the client with the player and the buyObj.Name.
Is there just a little mistake or can I not send arguments to the client at all? Any help will be appreciated!
The OnInvoke callback's first argument is always the player that fired it, so you should change line 1 of the first script to:
script.onTouch.OnInvoke = function(playerFired, button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
And :FireClient() requires a player argument as its first argument, so you should change
ui:FireClient(player, 'buyObj.Name')
to
ui:FireClient(playerFired, player, 'buyObj.Name')
Here is the solution I came up with;
First, I read through some Roblox documentation to find that the first argument I had to send when using :FireClient was the player, and because I already had the player there, it was just sending the function to that player. Now, I had 2 choices, I could send the player twice, or delete the player variable from the script. I chose the second one.
Here is what the :FireClient line in the server script looks like now:
game.ReplicatedStorage:WaitForChild('onPartLoad'):FireClient(player, buyObj.Name)
And here is what the client function script looks like now:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(name)
if name ~= 'starterFoundation' then
script.Parent.Text = name .. ' is loaded.'
end
end)
Thank you #TypeChecked for helping me realize this!

Set username/userID to boat when seat is occupied

I am working on a game in which you can sail with boats. My script works but I wonder if I am doing it right?
What I'm trying to do is that as soon as you spawn a boot the name of the boot is the userID + the name of the ship.
This works fine thanks to one guy on this forum (Much Love)!
So now I wanted to write a script inside the seat as soon as another player or the player himself sits on the chair changes the name of the boat to that player name + boat name. This works, but now I have managed to get the name of the player and not his id. I prefer the ID of the player. How do you do that?
Because I get the player name instead of his id. I had to make another check to see if the boat does not already exist. Now 1 player can get 2 boats because player.Name and player.UserID do not match.
So my question is how do I get the userID instead of its name from the script below so that a player can take over the boat but not get 2 boats in the end?
I'm sorry for my bad coding i'm new to this :) Thanks for helping out!
Script inside of seat:
local Seat = script.Parent
local debounceCheck = false
local ChangeBoatName = script.Parent.Parent
local boatname = "ShipOnePerson"
Seat.Changed:Connect(function(player)
if Seat.Occupant ~= nil then
if Seat.Occupant.Parent.Name ~= "PlayerNameHere" then
local PlayerToNewBoat = game.Players:FindFirstChild(Seat.Occupant.Parent.Name)
local Character = PlayerToNewBoat.Character or PlayerToNewBoat.CharacterAdded:Wait()
if not debounceCheck then
-- Is it Occupant?
debounceCheck = true
-- Mark it as Occupant, so that other handlers don't execute
if ChangeBoatName then
local plruserid = Seat.Occupant.Parent.Name
local SetNameToBoat = plruserid..boatname
local NewBoat = ChangeBoatName
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
print(NewBoat)
end
end
else
debounceCheck = false
-- Mark it as not Occupant, so other handlers can execute again
end
else
debounceCheck = false
-- Mark it as not Occupant, so other handlers can execute again
end
end)
My second question is how can I make sure that as soon as you press the button RegenButton it first checks if there is already a boat in that place and if so how do I make sure that you cannot spawn?
Below is the code that is in the button regenbutton
local cd = workspace.Regenbutton:WaitForChild('ClickDetector')
local boat = game:GetService('ServerStorage'):WaitForChild('ShipOnePerson')
local button = workspace:WaitForChild('Regenbutton')
local debounce = false
local boatname = "ShipOnePerson"
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function(player)
local plruserid = player.UserId
local SetNameToBoat = plruserid..boatname
local plrextraboat = player.Name..boatname
print(SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
local oldboat2 = workspace:FindFirstChild(plrextraboat)
if not debounce then
if oldboat then
oldboat:destroy()
end
if oldboat2 then
oldboat2:destroy()
end
debounce = true
local NewBoat = boat:Clone()
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)
So my question is how do I get the userID instead of its name
Instead of Player.Name use Player.UserId
My second question is how can I make sure that as soon as you press the button RegenButton it first checks if there is already a boat in that place and if so how do I make sure that you cannot spawn?
You could use Instance.GetChildren to get a list of all children, then iterate over that list in a loop. Check wether any of those children is a boat Instance.IsA and if so, check whether it is in the spawn location.

Roblox OnClick clothers giver script

pnts = script.Pants
shirt = script.Shirt
function onClicked(playerWhoClicked)
end
function GiveClothes(character)
if not character:findFirstChild("Shirt") then
shirt:Clone().Parent = character
else character:findFirstChild("Shirt"):Destroy()
shirt:Clone().Parent = character
end
if not character:findFirstChild("Pants") then
pnts:Clone().Parent = character
else character:findFirstChild("Pants"):Destroy()
pnts:Clone().Parent = character
end
end
game.Players.PlayerAdded:connect(function(p)
p.CharacterAdded:connect(function(char)
wait(1.12)
local plr = game.Players:findFirstChild(char.Name)
print(char.Name)
local groupid = 0 -- Id of your group
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then
if plr:IsInGroup(groupid) then
if plr:GetRoleInGroup(groupId) >= 50
then GiveClothes(char)
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
This script is supposed to give you certain clothes if you click on the button(Script is under the button and yes there is also a ClickDetector),but it only gives you the clothes if you are a certain rank in a Group.
But currently it does not work. How to fix this?
Avoid weird names like pnts, you will probably not die from adding the letter a
The ClickDetector already give you the player
And it is over all messy
Try this:
local Pants = script.Pants
local Shirt = script.Shirt
local GroupID = 42 -- Group id here
script.Parent.ClickDetector.MouseClick:connect(function(Player)
if not Player:IsInGroup(GroupID) then return end
if Player:GetRoleInGroup(GroupID) < 50 then return end
local Character = Player.Character
if Character == nil then return end
-- Get new shirt
local CharacterShirt = Character:findFirstChild("Shirt")
if CharacterShirt then CharacterShirt:Destroy() end
Shirt:Clone().Parent = Character
-- Get new pants
local CharacterPants = Character:findFirstChild("Pants")
if CharacterPants then CharacterPants:Destroy() end
Pants:Clone().Parent = Character
end)
Also make sure to post errors when you ask for help

Cloning+Dragging model when button is click ROBLOX [LUA]?

So I thought of having this for so long, I just don't know where to start. I am new to this language and I keep learning, but kind of hard for me. But I have built my very own custom character which took 2 weeks for me. Anyway, For my question. An example is if I have a button and I click it, a model will be clone and I can drag that model and put it anywhere nearby. What possible method I can use to achieve this?
First things first, I suggest for any future questions, you head over to https://scriptinghelpers.org/
now, on to your question, for cloning the model, you should use mouse.Target.Parent:Clone() or the GetTopParent(mouse.Target) function in my function library (which you can get here; http://www.roblox.com/item.aspx?id=244244638)
then deposit the model into workspace and MakeJoints()
the next step is to move the model, this can be tricky, but the simplest method is model:MoveTo(mouse.Hit.p) on mouse.Moved (but that's a little buggy)
Another method for movement would be to use the Handles class, but I'm not really familiar with it, so you'd have to figure that one out on your own.
To make the first method less buggy, I'd suggest something along the lines of
model:MoveTo(mouse.Hit.p.X, mouse.Target.Position.Y + (model:GetExtentsSize().Y / 2), mouse.Hit.p.Z)
but you'd have to set up the mouse to ignore the model, which I can't really help with.
A really good place to start is to search the free models in Studio Toolbox for a 'Dragger Tool' or 'Model Dragger Tool' and then use the script inside to get started creating your own. I have learned to create my own custom draggers by doing this and it is way easier than you may think at first. Once you find a good dragger tool to borrow code from, if you need to enhance it, you can find the dragger api in the Roblox Wiki to help you further customize it to your specific needs.
http://wiki.roblox.com/index.php?title=API:Class/Dragger
EDIT: So here's the first dragger script that showed when I searched. It will drag models and parts but you will have to edit it to meet your requirements using the dragger api. Create a Tool in player.BackPack then create a LocalScript inside the Tool then copy and paste the code below into the LocalScript, and that will get you started.
local Tool = script.Parent
enabled = true
local origTexture = Tool.TextureId
game:GetService("ContentProvider"):Preload("rbxasset://icons/freemove_sel.png")
local selectionBox
local currentSelection
local currentSelectionColors = {}
local selectionLasso
local inGui = false
local inPalette = false
local lockTime = 0
function canSelectObject(part)
return part and not (part.Locked) and (part.Position - script.Parent.Parent.Head.Position).Magnitude < 60
end
function findModel(part)
while part ~= nil do
if part.className == "Model" then
return part
end
part = part.Parent
end
return nil
end
function startDrag(mousePart, hitPoint, collection)
dragger = Instance.new("Dragger")
pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
end
function collectBaseParts(object, collection)
if object:IsA("BasePart") then
collection[#collection+1] = object
end
for index,child in pairs(object:GetChildren()) do
collectBaseParts(child, collection)
end
end
function onMouseDown(mouse)
mouse.Icon ="rbxasset://textures\\GrabRotateCursor.png"
local part = mouse.Target
if canSelectObject(part) then
local hitPoint = mouse.Hit:toObjectSpace(part.CFrame).p
if trySelection(part) then
local instances = {}
collectBaseParts(currentSelection, instances)
startDrag(part, hitPoint, instances)
return
end
end
--Clear the selection if we weren't able to lock succesfullu
onMouseUp(mouse)
end
function onMouseUp(mouse)
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
if dragger ~= nil then
pcall(function() dragger:MouseUp() end)
dragger = nil
end
end
function trySelection(part)
if canSelectObject(part) then
selectionLasso.Part = part
local model = findModel(part)
if model then
return setSelection(model)
else
return setSelection(part)
end
else
clearSelection()
return false
end
end
function onKeyDown(key)
if dragger ~= nil then
if key == 'R' or key == 'r' then
dragger:AxisRotate(Enum.Axis.Y)
elseif key == 'T' or key == 't' then
dragger:AxisRotate(Enum.Axis.Z)
end
end
end
local alreadyMoving
function onMouseMove(mouse)
if alreadyMoving then
return
end
alreadyMoving = true
if dragger ~= nil then
--Maintain the lock
if time() - lockTime > 3 then
Instance.Lock(currentSelection)
lockTime = time()
end
--Then drag
pcall(function() dragger:MouseMove(mouse.UnitRay) end)
else
trySelection(mouse.Target)
end
alreadyMoving = false
end
function saveSelectionColor(instance)
if instance:IsA("BasePart") then
currentSelectionColors[instance] = instance.BrickColor
if instance.BrickColor == BrickColor.Blue() then
instance.BrickColor = BrickColor.new("Deep blue")
else
instance.BrickColor = BrickColor.Blue()
end
end
local children = instance:GetChildren()
if children then
for pos, child in pairs(children) do
saveSelectionColor(child)
end
end
end
function setSelection(partOrModel)
if partOrModel ~= currentSelection then
clearSelection()
if Instance.Lock(partOrModel) then
lockTime = time()
currentSelection = partOrModel
saveSelectionColor(currentSelection)
selectionBox.Adornee = currentSelection
return true
end
else
if currentSelection ~= nil then
if time() - lockTime > 2 then
--Maintain the lock
if not(Instance.Lock(currentSelection)) then
--we lost the lock
clearSelection()
return false
else
lockTime = time()
return true
end
else
return true
end
end
end
return false
end
function clearSelection()
if currentSelection ~= nil then
for part, color in pairs(currentSelectionColors) do
part.BrickColor = color
end
selectionBox.Adornee = nil
Instance.Unlock(currentSelection)
end
currentSelectionColors = {}
currentSelection = nil
selectionLasso.Part = nil
selectionBox.Adornee = nil
end
function onEquippedLocal(mouse)
Tool.TextureId = "rbxasset://icons/freemove_sel.png"
local character = script.Parent.Parent
local player = game.Players:GetPlayerFromCharacter(character)
inGui = false
inPalette = false
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
mouse.Button1Down:connect(function() onMouseDown(mouse) end)
mouse.Button1Up:connect(function() onMouseUp(mouse) end)
mouse.Move:connect(function() onMouseMove(mouse) end)
mouse.KeyDown:connect(function(string) onKeyDown(string) end)
selectionBox = Instance.new("SelectionBox")
selectionBox.Name = "Model Delete Selection"
selectionBox.Color = BrickColor.Blue()
selectionBox.Adornee = nil
selectionBox.Parent = player.PlayerGui
selectionLasso = Instance.new("SelectionPartLasso")
selectionLasso.Name = "Model Drag Lasso"
selectionLasso.Humanoid = character.Humanoid
selectionLasso.archivable = false
selectionLasso.Visible = true
selectionLasso.Parent = game.workspace
selectionLasso.Color = BrickColor.Blue()
alreadyMoving = false
end
function onUnequippedLocal()
Tool.TextureId = origTexture
clearSelection()
selectionBox:Remove()
selectionLasso:Remove()
end
Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

Resources