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

Why does my punching script only work in edit mode? How can I fix this?

My punching script only works in edit mode for some reason. I can't seem to find what part of the script is messing things up. I know that scripts work differently in edit mode and play mode, but I still can't figure out why my script specifically won't work. Any help is appreciated!

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local Char = player.Character
local Humanoid = Char:WaitForChild("Humanoid")
local Trail = game.Workspace.Trail:Clone()
local Trail2 = game.Workspace.Trail:Clone()



local animations = {1877306614,1877291193}

local function Punch(plr)
local animation = Instance.new("Animation")
local picked = math.random(1, #animations)
animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked]
local animTrack = Humanoid:LoadAnimation(animation)
animTrack:Play()
local dmgScript = script.DmgScript:Clone()
if picked == 1 then
	dmgScript.Parent = Char.RightHand
	Trail.Parent = Char.HumanoidRootPart
            local attachment0 = Instance.new("Attachment", Char.RightHand)
            attachment0.Name = "TrailAttachment0"
            local attachment1 = Instance.new("Attachment", Char.RightUpperArm)
            attachment1.Name = "TrailAttachment1"
            Trail.Attachment0 = attachment0
            Trail.Attachment1 = attachment1	
            wait(0.5)
            Trail:Remove()
elseif picked == 2 then
	dmgScript.Parent = Char.LeftHand
	Trail2.Parent = Char.HumanoidRootPart
            local attachment0 = Instance.new("Attachment", Char.LeftHand)
            attachment0.Name = "TrailAttachment0"
            local attachment1 = Instance.new("Attachment", Char.LeftUpperArm)
            attachment1.Name = "TrailAttachment1"
            Trail2.Attachment0 = attachment0
            Trail2.Attachment1 = attachment1	
            wait(0.5)
            Trail2:Remove()
end
dmgScript.Disabled = false
wait(0.4)
dmgScript:Destroy()
end

Mouse.Button1Down:connect(function()
print("yup it worked")
Punch()
end)

^That's inside of a LocalScript inside of StarterGui

script.Parent.Touched:Connect(function(hit)
	local char = hit.Parent
	local hum = char:FindFirstChild("Humanoid")
	if hum and char.Name ~= script.Parent.Parent.Name then
		hum.Health = hum.Health - script.Dmg.Value
		script.Disabled = true
		wait(0.5)
		script.Disabled = false
	end
end)

^That's inside of a Server script inside of the script above

^And there's an IntValue inside of the script above

2 comments

0

Can you describe what happens when it "doesn't work"?

0

Literally nothing happens when I click

1 answer

✓ Accepted answer

Main reason of that kind of problems is because of failing local and server communication. Didn't read your script fully but if you want to do such thing you need to do communications well. Never use server script in a Gui, I suggest you to put it in ServerScriptService and connect to player from another way.

The ways I suggest: First one: Forming Your Code Better By using PlayerAdded and CharacterAdded events of Players service and Player object, you can reach to character to use Touched event of that... Part or something? You can check if player is punching from several ways but I leave it to your imagination. Find something both local can change and server can check.

Second one: Using a remote In this one you will have two scripts too, but one of them(Local one) will do all the work. Will wait for touched event etc. and when it detects and calculates and thinks everything is legit, it will fire a RemoteEvent(With FireServer) and server sided script will wait for ServerEvent. After it gets the signal it will damage the player and the work will be done.

3 comments

0

Aight I think I understand. I'm probably going to try the remoteEvent and see if I can get it to work. Thanks alot :)

0

Wishing the best :)

0

I got it to work. Thanks :D

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