I was making a punch script that everytime you press q the script in serverscriptservice has to choose an punch animation to play. The game is Filtering Enabled and is using R15. The reason why it's made this way is because I'm using another LocalScript in StarterGui to increase the value of the IntValue after a player transforms.
When I test the script in test mode, it works perfectly fine. However when i test it in the actual game the entire script plays but it doesn't damage the dummy.
LocalScript (StarterCharacterScripts)
01 | local uis = game:GetService( "UserInputService" ) |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | local punchEvent = ReplicatedStorage:WaitForChild( "PunchEvent" ) |
04 | local plr = game.Players.LocalPlayer |
05 | local char = plr.Character |
06 |
07 | local ready = true |
08 |
09 | local damageValue = Instance.new( "IntValue" , char) |
10 | damageValue.Name = "Damage" |
11 | damageValue.Value = 15 |
12 |
13 | local function punch(inputObject, gameProcessed) |
14 | if inputObject.KeyCode = = Enum.KeyCode.Q and ready then |
15 | punchEvent:FireServer() |
Script (ServerScriptService)
01 | math.randomseed(tick()) |
02 |
03 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local punchEvent = Instance.new( "RemoteEvent" , ReplicatedStorage) |
05 | punchEvent.Name = "PunchEvent" |
06 |
07 | local animations = { 1226591696 , 1226673643 } |
08 |
09 | local function onPunchFired(plr) |
10 | local char = game.Workspace:FindFirstChild(plr.Name) |
11 | local humanoid = char.Humanoid |
12 | local anim = Instance.new( "Animation" ) |
13 | local picked = math.random( 1 , #animations) |
14 | anim.AnimationId = "http://roblox.com/asset/?id=" ..animations [ picked ] |
15 | local animTrack = humanoid:LoadAnimation(anim) |
Damage Script (Inside of the script above)
01 | script.Parent.Touched:connect( function (hit) |
02 | local char = hit.Parent |
03 | local chara = script.Parent.Parent |
04 | local dmg = chara:FindFirstChild( "Damage" ) |
05 | local humanoid = char:FindFirstChild( "Humanoid" ) |
06 | if humanoid and char.Name ~ = script.Parent.Parent.Name and dmg then |
07 | humanoid.Health = humanoid.Health - dmg.Value |
08 | script.Disabled = true |
09 | wait( 0.5 ) |
10 | script.Disabled = false |
11 | end |
12 | end ) |
You created damageValue and parented it to Player.Character which is in workspace, and thus, since local scripts can't access workspace, it will not work.
Another question that you've asked in chat:
So currently, this is your damage script:
01 | script.Parent.Touched:connect( function (hit) |
02 | local char = hit.Parent |
03 | local chara = script.Parent.Parent |
04 | local dmg = chara:FindFirstChild( "Damage" ) |
05 | local humanoid = char:FindFirstChild( "Humanoid" ) |
06 | if humanoid and char.Name ~ = script.Parent.Parent.Name and dmg then |
07 | humanoid.Health = humanoid.Health - dmg.Value |
08 | script.Disabled = true |
09 | wait( 0.5 ) |
10 | script.Disabled = false |
11 | end |
12 | end ) |
In order to obtain the player, you can do one of the few steps:
01 | script.Parent.Touched:connect( function (hit) |
02 | local char = hit.Parent |
03 | --Use this |
04 | local player = char:GetPlayerFromCharacter() |
05 | local chara = script.Parent.Parent |
06 | local dmg = chara:FindFirstChild( "Damage" ) |
07 | local humanoid = char:FindFirstChild( "Humanoid" ) |
08 | if humanoid and char.Name ~ = script.Parent.Parent.Name and dmg then |
09 | humanoid.Health = humanoid.Health - dmg.Value |
10 | script.Disabled = true |
11 | wait( 0.5 ) |
12 | script.Disabled = false |
13 | end |
14 | end ) |
If #1 shows an error, you can also do this:
Since you do know char's name, and since the char's name and name of player in the folder are the same, you can do this:
01 | script.Parent.Touched:connect( function (hit) |
02 | local char = hit.Parent |
03 | --Find for name in players |
04 | local player = game.Players:FindFirstChild(hit.Parent.Name) |
05 | local chara = script.Parent.Parent |
06 | local dmg = chara:FindFirstChild( "Damage" ) |
07 | local humanoid = char:FindFirstChild( "Humanoid" ) |
08 | if humanoid and char.Name ~ = script.Parent.Parent.Name and dmg then |
09 | humanoid.Health = humanoid.Health - dmg.Value |
10 | script.Disabled = true |
11 | wait( 0.5 ) |
12 | script.Disabled = false |
13 | end |
14 | end ) |
Additionally, some other spotted mistakes:
You cannot disable a script and reenable it again. Since the script "Suicided" itself, it can not restart itself since it is stopped from continuing if it is disabled.
The last part will not work, but that's okay! Touched events fire every time the part is touched.