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

Why do my two scripts work in studio's test mode but not in actual online gameplay?

Edit: Fixed script indenting I think Edit 2: I'm pretty sure the problem is in the "hurt" script, but I put the attack script in just in case. Edit 3: Made indenting a little better. Sorry! D:

There are two scripts here: an attack script in StarterPack and a hurt script in a model in the workspace. What's supposed to happen is when the player attacks, a "state" value inside the character changes to 1, and the hurt script detects if there is a humanoid and the state is = 1, and kills the enemy. However, this only seems to work when playing in studio. Here are the two scripts:

attack (in StarterPack) (it's a modified double jump script)

wait(5)
local plyr = game.Players.LocalPlayer

plyr.Character:WaitForChild("Humanoid")
db=false
j=0
local InputKey = "E"
deb=false
J1=false
J2=false
local UserInputService = game:GetService("UserInputService")
char = plyr.Character
UserInputService.InputBegan:connect(function(UserInput)
	if UserInput.UserInputType == Enum.UserInputType.Keyboard then

		if UserInput.KeyCode == Enum.KeyCode[InputKey] and J1==true and deb==false and j==1 and plyr.Character.Torso.Velocity.Y~=0 and db==false then
			j=2
			deb=true
			plyr.Character.Torso.Velocity=plyr.Character.Torso.CFrame.lookVector*30 + Vector3.new(0,30,0)
			local pe=script.ParticleEmitter:Clone()
			pe.Parent=plyr.Character.Torso
			script.Sound:Play()
			local w = plyr.Character.Humanoid:LoadAnimation(script:WaitForChild("Animation"))
			wait(0.1)
			pe.Enabled = false
			local state = plyr.Character:WaitForChild("state")
			state.Value = "1"
			print("attack is in the character")
			w:Play()
			repeat wait() until script.Sound.IsPlaying == false
			w:Stop()
			pe:Destroy()
			state.Value = "0"
			print("attack was removed")
			J2=true
			deb=false
		end
	end
end)

function D()
	if  J1==false then
		db=true
		J1=true
		j=1
		db=false
	end
end
plyr.Character.Humanoid.Jumping:connect(D)

while wait() do
	if plyr.Character.Torso.Velocity.Y>-.1 and plyr.Character.Torso.Velocity.Y<.1 then
		J1=false
		j=0
	end
end

hurt (inside of a Part in a Model)

debounce = true
function onTouched(hit)
	local hh = hit.Parent:FindFirstChild("Humanoid")
		if hh ~= nil then
			hit.Parent:WaitForChild("state")
				if hit.Parent.state.Value == 1 then
					print("found value = 1")
					local	qwe = Instance.new("IntValue")
					qwe.Name = "attacked"
					qwe.Parent = script.Parent
						if debounce == true then
							if	script.Parent.attacked ~= nil then
								print("attacking human found")
								debounce = false
								script.Parent.Parent.Script.Disabled = true
								script.Parent.Parent.Animate.Disabled = true
								local w = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Parent.die)
								w:Play()
								script.Parent.hurts:Play()
								script.Parent.BeautifulSparkles.Enabled=true
								wait(1)
								script.Parent.BeautifulSparkles.Enabled=false
								script.Parent.Parent.Parent.cloud.Script.Disabled = false
								script.Parent.Sound:Play()
								wait(1)
								script.Parent.Transparency = 1
								script.Parent.PointLight:Destroy()
								script.Parent.Decal.Transparency = 1
								script.Parent.Parent.LowerLeg1.Transparency = 1
								script.Parent.Parent.LowerJoint1.Transparency = 1
								script.Parent.Parent.UpperLeg1.Transparency = 1
								script.Parent.Parent.Torso.Transparency = 1
								wait(1)
								debounce = true
							end
						end
				end
				else
				print("touched but not attacked")
		end
end
script.Parent.Touched:connect(onTouched)

Also, I'm a noob scripter so if the answer is super obvious then good for you, I guess.

3 comments

1

Please indent your code properly.

0

it's because you need Remote Events/Remote functions. Look them up on the wiki for more info I don't have enough room to explain them

0

you definitely did not fix your script's indenting.. it gives me a headache just looking at that

1 answer

✓ Accepted answer

It will not work because it's not readable by the server.

Try tiding up your scripts and make them look nice.

For example, line 12 of the second script:

if  script.Parent.attacked ~= nil then

Should be

if script.Parent.attacked ~= nil then

Line 42 of the first script

if  J1==false then

Should be

if J1 == false then

Hope that will fix your problem!

2 comments

0

You can tidy up the if statement even more to: if script.Parent.attacked then. I don't think just changing how much space you have between characters will effect code.

0

Thanks! :D This really helped.

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