killette2Join Date: 2009-09-16 Post Count: 3502 |
I have made a simple .Touched script but I do not know how to get it to repeat itself as long as you are touching the brick it is in. What would I do to go about that?
--------------------------
function onTouched(hit)
check = hit.Parent:FindFirstChild("Humanoid")
if check ~= nil then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
local stats = user:findFirstChild("leaderstats")
if stats ~= nil then
local energy = stats:findFirstChild("Energy")
energy.Value = energy.Value + 1
wait(3)
end
end
end
script.Parent.Touched:connect(onTouched) |
|
killette2Join Date: 2009-09-16 Post Count: 3502 |
Should I use a while true function? I'm a bit lost |
|
|
Use a debounce
http://wiki.roblox.com/index.php?title=Debounce
I hate the LGBT. Those laser guided battle tanks are just too damn powerful. |
|
KAAK82Join Date: 2014-05-06 Post Count: 1166 |
local Debounce = false
function Test()
if not Debounce then
Debounce = true
--Code
Debounce = false
end |
|
|
I think he wants a code that repeats as long as a player is touching the part, not a debounce script.
TouchEnd = true
script.Parent.Touched:connect(function (hit)
check = hit.Parent:FindFirstChild("Humanoid")
if check ~= nil then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
local stats = user:findFirstChild("leaderstats")
if stats ~= nil then
TouchEnd = false
while TouchEnd == false do
local energy = stats:findFirstChild("Energy")
energy.Value = energy.Value + 1
wait(3)
end
end
end
end)
game.Workspace.Part.TouchEnded:connect(function()
TouchEnd = true
end)
Tested this using health increase; seems to work. |
|
DataStoreJoin Date: 2012-02-07 Post Count: 8540 |
@Nobbers, KAAK,
Did either of you actually read what Killette2 posted? Or did you just go by the title? He wasn't asking how to do a debounce, but rather to get something to repeat continuously until an object stopped touching another.
@OP,
Yes, you could use a loop. To get when something stops touching an object you can use the TouchEnded event. |
|
killette2Join Date: 2009-09-16 Post Count: 3502 |
Thank you frost, I was wondering about the touchend feature. Unfortunately this doesn't seem to be stopping when I stop touching said brick and it also gives me the stat a lot more rapidly than once every 3 seconds, would I need a debounce for this? |
|
|
Yes, you would then need debounce, because the action fires every single time it notices you are in contact.
I hate the LGBT. Those laser guided battle tanks are just too damn powerful. |
|
killette2Join Date: 2009-09-16 Post Count: 3502 |
So I messed around more with what I was given and it seems to do it once every 3 seconds, but this happens for every time it is touched ever with any body part. If it is touched 1 time with an arm, it works correctly, but if left and touched again it works incorrectly, as it then gives 2 per 3 seconds. If you jump and rub all over it, the number then ticks up rapidly never stopping. |
|
|
I suppose you could add in another BoolValue upon touching the brick which is tested for once another part such as an arm touches the brick.
For example,
script.Parent.Touched:connect(function (part)
value = part.Parent:findFirstChild("BOOLVALUEHERE")
if value ~= true then
bool = Instance.new("BoolValue")
bool.Parent = part.Parent
--Codeycodey
end
end)
script.Parent.TouchEnd:connect(function (part)
part.Parent.BOOLVALUEHERE:Destroy()
end)
This isn't a script for your situation exactly, but an idea of what you can do yourself. |
|