Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why are my scripts not working?!

I tried to make a gui that resets the player, and gives them a value of 1. That value I used in my second script, which would give them a specific weapon based on if they clicked the gui. Script 1:

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent

local textButton = Instance.new("TextButton")
textButton.Parent = screenGui
textButton.Position = UDim2.new(.25, 0, .25, 0)
textButton.Size = UDim2.new(.1, 0, .1, 0)
textButton.BackgroundColor3 = BrickColor.White().Color
textButton.Text = "Peasant"

textButton.Mousebutton1down:connect(function()  
	game.Players.LocalPlayer.Character.Humanoid = 1
	game.Players.LocalPlayer.Character.Humanoid.Health = 0
end)

Script 2:

if game.Players.LocalPlayer.Character.Humanoid == 1 then
	game.Lighting["Pitchfork"]:clone().Parent = game.Players.LocalPlayer.Character.Backpack
end

I'm pretty sure it because I messed up with the identifier of 1, I'm not sure if condition statement work with my script. Can someone plz help me! Also, why does clicking the GUI not reset the character? And is there anyway for the gui to disappear after it was clicked?!

2 answers

.Humanoid is a child, not a property -- you cannot set a child.

Also, when the Character dies, everything that was in it is obliterated. So if you want to communicate to the next spawn, you won't be able to save anything in the Character.


You can instead create a new object, and keep it in the player.

That might look like this:

-- A new Script in ServerScriptService
-- Gives every player the StringValue to save their role:
function newPlayer(player)
	local value = Instance.new("StringValue", player)
	value.Name = "Role"
end
game.Players.PlayerdAdded:connect(newPlayer)

-- This helps with testing offline:
for _, player in pairs(game.Players:GetPlayers()) do
	newPlayer(player)
end

(You only want this to be done once -- not each spawn -- so this has to be done when the player joins the game)


Next, the button sets the Role:

textButton.Mousebutton1down:connect(function() 
	game.Players.LocalPlayer.Role.Value = "Peasant"
	game.Players.LocalPlayer.Character.Humanoid.Health = 0
end

and the other script checks it: (The backpack is not in the Character -- it's in the player)

if game.Players.LocalPlayer.Role.Value == "Peasant" then
	game.Lighting["Pitchfork"]:clone().Parent = game.Players.LocalPlayer.Backpack
end

The moment the player spawns, the Backpack isn't going to exist -- you should probably wait for it:

....Parent = game.Players.LocalPlayer:WaitForChild("Backpack")

Better Methods

While the above should work, there are slight ways to make it more consistent with general ROBLOX style.

  • You can use .Pitchfork instead of ["Pitchfork"]
  • You should use :Clone() instead of :clone()
  • You should use ReplicatedStorage instead of Lighting to keep models

There is a :LoadCharacter() method of players that will gracefully spawn a new player without killing them. Unfortunately, it has to be called from a Script, not a LocalScript. You could use a RemoteEvent or RemoteFunction to use this.

Your Problem

You're trying to set the Humanoid object to one.. That doesn't make any sense. Try making a vlue in the humanoid and checking that.

Also, in your second script you're doing some things wrong

  • 1: An if statement does not wait for the condition to happen, you need to use an Event. Normally, you would use an event but in your case then you don't know when the object will appear so you have to use a loop and check the value.

  • 2: The Backpack is not in the Character! It is in the Player.


Code1

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent

local textButton = Instance.new("TextButton")
textButton.Parent = screenGui
textButton.Position = UDim2.new(.25, 0, .25, 0)
textButton.Size = UDim2.new(.1, 0, .1, 0)
textButton.BackgroundColor3 = BrickColor.White().Color
textButton.Text = "Peasant"

textButton.Mousebutton1down:connect(function()  
	local hum = game.Players.LocalPlayer.Character.Humanoid
	local val = Instance.new('IntValue',hum) --Create a value
	val.Name = 'Check'
	val.Value = 1
	 hum.Health = 0
end)

Code2

local plr = game.Players.LocalPlayer
local val = plr.Character.Humanoid:FindFirstChild('Check')
local tool = game.Lighting.Pitchfork

while wait(1) do
	if val and val.Value == 1 then
		tool:Clone().Parent = plr.Backpack
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.