Hello! I’m trying to make a simple system that damages the player when touched. It takes away one health point, and then sets a BoolValue to true. Another script takes care of all the custom states I gave my player. I set the hit value to true, and the invincibility value to true. The invincibility value does nothing to protect the player, it simply stops anything from executing that harms the player. The invincibility goes away after 5 seconds.
The hit value does absolutely nothing but anchor the player (to play an animation), activates the Invincible bool, and plays a few sounds.
However, my problem arises whenever I deactivate the Invincibility bool. The script acts like it’s still on, even though its not! I made it print something, and it still prints whenever the bool is off.
Note: I would use forcefields, but I am using a completely custom StarterCharacter, and forcefields don’t appear properly on the player.
Another note: The script got pretty messy due to me messing around with solutions that ended up not working.
function debounce(func)
local isRunning = false -- Create a local debounce variable
return function(...) -- Return a new function
if not isRunning then
isRunning = true
func(...) -- Call it with the original arguments
isRunning = false
end
end
end
function Damage()
Parent.Humanoid:TakeDamage(1)
Parent.CustomStates.Pained.Value = true
Parent.CustomStates.Invincible.Value = true
end
local CollectionService = game:GetService("CollectionService")
local Tag = CollectionService:GetTagged("GenericHarmful")
for _, Tag in pairs(Tag) do
local function PlayerTouched(Part)
Parent = Part.Parent
if game.Players:GetPlayerFromCharacter(Parent) and Parent.CustomStates.Invincible.Value == false then
Damage()
else
print("touched with invincibility")
end
end
Tag.Touched:connect(debounce(PlayerTouched))
end
Thanks for helping!
2 Likes
Where exactly are you making Invinsible false? Perhaps I’m just missing it, but I just see you make it true here:
function Damage()
Parent.Humanoid:TakeDamage(1)
Parent.CustomStates.Pained.Value = true
Parent.CustomStates.Invincible.Value = true
end
The only other time I see Invisible pop up is here:
for _, Tag in pairs(Tag) do
local function PlayerTouched(Part)
Parent = Part.Parent
if game.Players:GetPlayerFromCharacter(Parent) and Parent.CustomStates.Invincible.Value == false then
Damage()
else
print("touched with invincibility")
end
end
Tag.Touched:connect(debounce(PlayerTouched))
end
Turning invincibility off is in another script that handles what to do when a BoolValue is activated. Here is a snippet:
function InvincibleFlash()
Invincible.Value = true
wait(5)
Invincible.Value = false
print("INVINCIBILITY ENDED")
end
Hit.Changed:connect(function()
if Hit.Value == true then
spawn(InvincibleFlash)
StopALLMovements()
wait(1)
RegainALLMovements()
end
end)
1 Like
The way you’re doing this doesn’t conflict with one another? I mean, having both scripts change the bool value to true.
1 Like
It doesnt, I can check my player and I can see that the Invincibility BoolValue turns off after 5 seconds, like what I need. I’m sorry if I wasn’t clear, but my problem is getting it to find out if the value changed once touched.
2 Likes
Wait, aren’t you just setting the bool value back to true when it becomes false? Or is this supposed to happen seconds after?
1 Like
Also, from what I remember, you need to make it a capital C for :Connect. Change it to:
Tag.Touched:Connect(debounce(PlayerTouched))
Really? Some scripts usually have it lowercase.
Anyways, I tried removing the Invincible.Value = true line in the Damage function. It still doesn’t work for some reason.
Odd… I have an online class I gotta attend to, so I’ll see if I can help later. I hope that you can get the problem fixed in the meanwhile!
1 Like
Question, is there a local script changing the value?
If a local script changes the value then the server script won’t know it changed.
It is a local script. I guess I can use RemoteEvents to fix this. I’ll update you guys once I try this!
This worked! I moved some of the state manager’s code to a server script. Thanks a lot!
Ah nice, good to know it was fixed. Have a great weekend!
1 Like