Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why won't my script damage a dummy in the actual game?

Asked by 7 years ago

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)

01local uis = game:GetService("UserInputService")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")
04local plr = game.Players.LocalPlayer
05local char = plr.Character
06 
07local ready = true
08 
09local damageValue = Instance.new("IntValue", char)
10damageValue.Name = "Damage"
11damageValue.Value = 15
12 
13local function punch(inputObject, gameProcessed)
14    if inputObject.KeyCode == Enum.KeyCode.Q and ready then
15        punchEvent:FireServer()
View all 22 lines...

Script (ServerScriptService)

01math.randomseed(tick())
02 
03local ReplicatedStorage = game:GetService("ReplicatedStorage")
04local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
05punchEvent.Name = "PunchEvent"
06 
07local animations = {1226591696, 1226673643}
08 
09local 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)
View all 28 lines...

Damage Script (Inside of the script above)

01script.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)

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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:

01script.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:

1: Use GetPlayerFromCharacter()

01script.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:

2: Search for the character in players by name

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:

01script.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.

Ad

Answer this question