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

I am struggling with my if statements and then statements ?

local PartTouch = 1

game.Workspace.Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then if PartTouch ==
			1 then 
			local keys = hit.leaderstats.keys keys.Value = keys.Value + 1 
			
		end
	end
end) 

and IT GIVES ME ERRORS

3 comments

0

dude, can you arrange you code well put it inside the Squiggly lines when you click the code block button

0

what is the error?

0

They have put a space in between the "keys keys" part.

1 answer

Hello.

You cannot have spaces be used like that when you are trying to add to values. I also recommend adding a Debounce to your script, so that the values aren't added more than once.

Assuming that your leaderstats is located in the Player's character, this is what the script should look like:

local PartTouch = 1
local Debounce = false

local Players = game:GetService("Players")
local Cooldown = 1 -- Can be changed to how much of a cooldown you want before the part can be touched again. This is not related to changing values in the leaderstats.

local function PartTouched(Collision)
	if not (Debounce) then
		Debounce = true
		 local Humanoid = Collision.Parent:FindFirstChildWhichIsA("Humanoid")
		 if (Humanoid) then  -- Verify that whatever touched this part has a humanoid.

		   local Player = Players:GetPlayerFromCharacter(Collision.Parent)
		   if (Player) then -- Verify that whatever touched this part is a player and has a humanoid.
			local Leaderstats = Player.Character.leaderstats
			local Keys = Leaderstats:WaitForChild("keys keys")

			Keys.Value += 1
		  end
		end
		wait(Cooldown)
		Debounce = false
	end
end

workspace.Part.Touched:Connect(PartTouched)

Hope this works for you.

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