Hello! You must've heard me from my previous question about the landmine script problem (or not). Well I took advice from blowup999 (the person that helped me with my landmine script). It looked like my game was done, until when I created a server, some scripts were not working: my landmine script and my Press R to reset script. They were working normally on ROBLOX Studio test but not on an actual server. Note: Both scripts are normal, not localscript. LANDMINE SCRIPT:
01 | canExplode = true ; |
02 | function ExplodePart(part) |
03 | if canExplode then |
04 | canExplode = false |
05 | game.Players.LocalPlayer.Character.Humanoid.Health = 0 |
06 | local explosion = Instance.new( "Explosion" ) |
07 | explosion.BlastRadius = 50 |
08 | explosion.Position = part.Position |
09 | explosion.Parent = game.Workspace |
10 | wait( 1 ) |
11 | canExplode = true |
12 | end |
13 | end |
14 |
15 | landMine = game.Workspace.Landmine |
16 |
17 | landMine.Touched:connect(ExplodePart) |
PRESS R TO RESET SCRIPT:
1 | function onKeyPress(actionName, userInputState, inputObject) |
2 | if userInputState = = Enum.UserInputState.Begin then |
3 | game.Players.LocalPlayer.Character.Humanoid.Health = 0 |
4 | print ( "Dead" ) |
5 | end |
6 | end |
7 |
8 | game.ContextActionService:BindAction( "keyPress" , onKeyPress, false , Enum.KeyCode.R) |
Once again, please help a beginner scripter! :)
Several things are wrong here. For one, you're attempting to essentially get input from the server. As the server isn't a player, and has no inputs to be bound to (Thru ContextActionService, or UserInputService) so your scripts are not working. You're also using direct references(EX: workspace.part.whatever) which isn't recommended, and will eventually cause you even more problems. For future reference, make things relative whenever you can (EX:script.Parent, which would act on the script you run it from's parent, so it doesn't matter if the part's hierarchy changes).
The code reworked:
Landmine:
01 | canExplode = true ; |
02 | function ExplodePart(part) |
03 | if canExplode and part.Parent:FindFirstChild( "Humanoid" ) then |
04 | local player = game.Players:GetPlayerFromCharacter(part.Parent) |
05 | canExplode = false |
06 | player.Character.Humanoid.Health = 0 |
07 | local explosion = Instance.new( "Explosion" ) |
08 | explosion.BlastRadius = 50 |
09 | explosion.Position = part.Position |
10 | explosion.Parent = game.Workspace |
11 | wait( 1 ) |
12 | canExplode = true |
13 | end |
14 | end |
15 |
16 | landMine = script.Parent --Use this if script is child of your landmine, otherwise replace with direct reference(workspace.part.blabla) |
17 |
18 | landMine.Touched:connect(ExplodePart) |
Explanation: All I've done here is replaced one of your direct references, with a system that determines if it was a player that touched the part. It goes to the part's parent, and then checks if there is a player that owns that model. If there is, it gets the player object using GetPlayerFromCharacter, and then applies the damage to that. If there is no player attached to that model, the second argument of the if statement is nil, so none of the code is run and no harm done.
Second Script:
This one is all correct, you just have it in the wrong script type and place. What you need to do is put the code into a localScript, which you then manually insert into StarterPlayerScripts (accessed thru game>StarterPlayer>StarterPlayerScripts)
Hope this helped!