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

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

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)


local uis = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")
local plr = game.Players.LocalPlayer
local char = plr.Character

local ready = true

local damageValue = Instance.new("IntValue", char)
damageValue.Name = "Damage"
damageValue.Value = 15

local function punch(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.Q and ready then
		punchEvent:FireServer()
		ready = false
		wait(0.5)
		ready = true
	end
end

uis.InputBegan:Connect(punch)

Script (ServerScriptService)

math.randomseed(tick())

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"

local animations = {1226591696, 1226673643}

local function onPunchFired(plr)
	local char = game.Workspace:FindFirstChild(plr.Name)
	local humanoid = char.Humanoid
	local anim = Instance.new("Animation")
	local picked = math.random(1, #animations)
	anim.AnimationId = "http://roblox.com/asset/?id="..animations[picked]
	local animTrack = humanoid:LoadAnimation(anim)
	animTrack:Play()
	local damageScript = script.DamageScript:Clone()
	if picked == 1 then
		damageScript.Parent = char.RightHand
	elseif picked == 2 then
		damageScript.Parent = char.LeftHand
	end
	damageScript.Disabled = false
	wait(0.4)
	damageScript:Destroy()
end

punchEvent.OnServerEvent:connect(onPunchFired)

Damage Script (Inside of the script above)

script.Parent.Touched:connect(function(hit)
	local char = hit.Parent
	local chara = script.Parent.Parent
	local dmg = chara:FindFirstChild("Damage")
	local humanoid = char:FindFirstChild("Humanoid")
	if humanoid and char.Name ~= script.Parent.Parent.Name and dmg then
		humanoid.Health	 = humanoid.Health - dmg.Value
		script.Disabled  = true
		wait(0.5)
		script.Disabled = false
		end
	end)

1 answer

✓ Accepted answer

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:

script.Parent.Touched:connect(function(hit)
    local char = hit.Parent
    local chara = script.Parent.Parent
    local dmg = chara:FindFirstChild("Damage")
    local humanoid = char:FindFirstChild("Humanoid")
    if humanoid and char.Name ~= script.Parent.Parent.Name and dmg then
        humanoid.Health  = humanoid.Health - dmg.Value
        script.Disabled  = true
        wait(0.5)
        script.Disabled = false
        end
    end)

In order to obtain the player, you can do one of the few steps:

#1: Use GetPlayerFromCharacter()

script.Parent.Touched:connect(function(hit)
    local char = hit.Parent
--Use this
    local player = char:GetPlayerFromCharacter()
    local chara = script.Parent.Parent
    local dmg = chara:FindFirstChild("Damage")
    local humanoid = char:FindFirstChild("Humanoid")
    if humanoid and char.Name ~= script.Parent.Parent.Name and dmg then
        humanoid.Health  = humanoid.Health - dmg.Value
        script.Disabled  = true
        wait(0.5)
        script.Disabled = false
        end
    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:

script.Parent.Touched:connect(function(hit)
    local char = hit.Parent
--Find for name in players
    local player = game.Players:FindFirstChild(hit.Parent.Name)
    local chara = script.Parent.Parent
    local dmg = chara:FindFirstChild("Damage")
    local humanoid = char:FindFirstChild("Humanoid")
    if humanoid and char.Name ~= script.Parent.Parent.Name and dmg then
        humanoid.Health  = humanoid.Health - dmg.Value
        script.Disabled  = true
        wait(0.5)
        script.Disabled = false
        end
    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.

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