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

I am trying to make a script which activates on touch?

I am trying to make a script which activates leaderstats on touch, but I keep on getting this error. 20:36:18.284 - Touched is not a valid member of Workspace 20:36:18.285 - Stack Begin 20:36:18.285 - Script 'Workspace.Script', Line 10 20:36:18.286 - Stack End

and this is my full script

function onTouched(part)
game.Players.PlayerAdded:connect(function(player)
     local leaderstats = Instance.new("Model", player)
     leaderstats.Name = "leaderstats"
     leaderstats.Parent = player

     local money = Instance.new("IntValue") --We create a new IntValue
     money.Name = "Stage" --this is the name you want the leader-stat to be when it shows up in-game.
     money.Value = 50 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
     money.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
     

local Timer = Instance.new("IntValue") --We create a new IntValue
     Timer.Name = "Time" --this is the name you want the leader-stat to be when it shows up in-game.
     Timer.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
     Timer.Parent = leaderstats -- Set the money object as a child of the leaderstats object.

     while true do 
     wait(1)
     Timer.Value = Timer.Value +1

 local Deaths = Instance.new("IntValue") --We create a new IntValue
     Deaths.Name = "Deaths" --this is the name you want the leader-stat to be when it shows up in-game.
     Deaths.Value = 25 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
     Deaths.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
player.CharacterAdded:connect(function(Character)
	Character:WaitForChild("Humanoid").Died:connect(function()
		Deaths.Value = Deaths.Value - 1
		if Deaths.Value == 0 then
local	msg = Instance.new("Message")
        msg.Parent = game.Workspace
        msg.Text = "GAME OVER Try again"--this is an example
        wait(2.5) -- change this time to however many seconds you want the message to remain
        msg:remove()
	

		end
		script.Parent.Touched:connect(onTouched)
		end)
	end)
    end
	end)
	end

I am making a special kind of obby.

Help is appreciated

1 answer

Your tabbing is really off and it seems to have messed up your whole script in general

Please just let roblox tab for you and don't mess with it in the future

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Model"); leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("IntValue") --We create a new IntValue
	money.Name = "Stage" --this is the name you want the leader-stat to be when it shows up in-game.
	money.Value = 50 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
	money.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
	
	
	local Timer = Instance.new("IntValue") --We create a new IntValue
	Timer.Name = "Time" --this is the name you want the leader-stat to be when it shows up in-game.
	Timer.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
	Timer.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
	
	local Deaths = Instance.new("IntValue") --We create a new IntValue
	Deaths.Name = "Deaths" --this is the name you want the leader-stat to be when it shows up in-game.
	Deaths.Value = 25 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
	Deaths.Parent = leaderstats -- Set the money object as a child of the leaderstats object.
	player.CharacterAdded:connect(function(Character)
		Character:WaitForChild("Humanoid").Died:connect(function()
			Deaths.Value = Deaths.Value - 1
			if Deaths.Value == 0 then
				local   msg = Instance.new("Message")
				msg.Parent = game.Workspace
				msg.Text = "GAME OVER Try again"--this is an example
				wait(2.5) -- change this time to however many seconds you want the message to remain
				msg:remove()
				Timer.Value = 0
				Deaths.Value = 25
			end
		end)
	end)
	
	while true do --This loop runs endlessly, it must be in the bottom of the execution
		wait(1)
		Timer.Value = Timer.Value + 1
	end
end)

Tested this in-game and it works fine, there's no need for a touch event anyway

4 comments

0

i need the touch the touch event because i am making people race, so it is timed

0

You could use any event, just change PlayerAdded

0

I am new to scripting so could you please explain on a script. Thank you sooo much. Btw Your script worked better than i thought it would.

0

I tried what you said, i replaced the 1st line (PlayerAdded) with (Touched) and it gave me an error " Touched is not valid member of Players

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